不兼容的形状:[1020,1,1]与[1019,1,1]-Tensorflow

问题描述 投票:0回答:1

[将神经网络的输出设置为1时出现问题,它在数据数组中出现问题,指责哪种形状更大,如果我使用periods = len(valuesAnalisys) - 1,一切正常!

句号:

periods = 1

返回:

Imcompatible shapes: [1020,1,1] vs. [1019,1,1]

数据集:

datecollect = [x[0] for x in dataSet]
servers = [x[1] for x in dataSet]
valuesAnalisys = [float(x[2]) for x in dataSet]

base = np.array(valuesAnalisys)

periods = len(valuesAnalisys) - 1
future_forecast = 1

X = base[0:(len(base) - (len(base) % periods))]
X_batches = X.reshape(-1, periods, 1)

y = base[1:(len(base) - (len(base) % periods)) + future_forecast]
y_batches = y.reshape(-1, periods, 1)

X_test = base[-(periods + future_forecast):]
X_test = X_test[:periods]
X_test = X_test.reshape(-1, periods, 1)
y_test = base[-(periods):]
y_test = y_test.reshape(-1, periods, 1)

tf.reset_default_graph()

appetizer = 1
hidden_neurons = 100
exit_neurons = 1

xph = tf.placeholder(tf.float32, [None, periods, appetizer])
yph = tf.placeholder(tf.float32, [None, periods, exit_neurons])

cell = tf.contrib.rnn.BasicRNNCell(num_units = hidden_neurons, activation = tf.nn.relu)

cell = tf.contrib.rnn.OutputProjectionWrapper(cell, output_size = 1)

exit_rnn, _ = tf.nn.dynamic_rnn(cell, xph, dtype = tf.float32)
calculateError = tf.losses.mean_squared_error(labels = yph, predictions = exit_rnn)
otimizador = tf.train.AdamOptimizer(learning_rate = 0.001)
training = otimizador.minimize(calculateError)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(2000):
        _, cost = sess.run([training, calculateError], feed_dict = {xph: X_batches, yph: y_batches})
        if epoch % 100 == 0:
            print("[INFO] Epoch: {} - Level Error: {}".format(epoch,cost))

    forecast = sess.run(exit_rnn, feed_dict = {xph: X_test})

y_test.shape
y_test2 = np.ravel(y_test)

final_forecast = np.ravel(forecast)

mae = mean_absolute_error(y_test2, final_forecast)

for (host, forecast, date) in list(zip(servers, final_forecast, datecollect)):
    send.postForecastMemory(host, forecast, cost, date)

型号:

hostname,"itemid","itemname","itemkey","historyvalue","datecollect","dateinsert"
serveNFE,30517,Uso do processador na média de 1 minuto,"system.cpu.load[percpu,avg1]",43,2020-04-04 06:00:00,2020-04-04 17:23:42
serveNFE,30517,Uso do processador na média de 1 minuto,"system.cpu.load[percpu,avg1]",51,2020-04-04 06:01:00,2020-04-04 17:23:42
serveNFE,30517,Uso do processador na média de 1 minuto,"system.cpu.load[percpu,avg1]",35,2020-04-04 06:02:00,2020-04-04 17:23:42
serveNFE,30517,Uso do processador na média de 1 minuto,"system.cpu.load[percpu,avg1]",32,2020-04-04 06:03:00,2020-04-04 17:23:42
serveNFE,30517,Uso do processador na média de 1 minuto,"system.cpu.load[percpu,avg1]",35,2020-04-04 06:04:00,2020-04-04 17:23:42

Details:用于分析的列为historyvaluedatecollect

Shape matriz:

[INFO] base shape: (1020,)
[INFO] X shape: (1019,)
[INFO] X batches shape: (1, 1019, 1)
[INFO] Y batches shape: (1, 1019, 1)
[INFO] X test shape: (1, 1019, 1)
[INFO] Y test shape: (1, 1019, 1)
python tensorflow deep-learning shapes forecast
1个回答
2
投票

罪魁祸首似乎是您的RNN单元中固定的时间维度。

xph = tf.placeholder(tf.float32, [None, periods, appetizer])
yph = tf.placeholder(tf.float32, [None, periods, exit_neurons])

cell = tf.contrib.rnn.BasicRNNCell(num_units = hidden_neurons, activation = tf.nn.relu)

这里,在xph和yph中,您都将时间维指定为周期。因此,如果您的信号更长或更短,则会出现错误。

由于您未指定输入形状或模型摘要,因此我无法推断出模型层的确切尺寸。因此,使用占位符数字。

有两个可能的修复。

  1. 不是使用固定的时间维=周期,而是使用None。
xph = tf.placeholder(tf.float32, [None, None, appetizer])
yph = tf.placeholder(tf.float32, [None, None, exit_neurons])

但是,缺点是每个批次中的信号长度必须相同,或者您可以简单地使用批次大小= 1进行训练,而不必担心时间长度。

  1. 使用截断/填充来解决长度问题。只需将信号传递给预处理功能即可添加/删除额外的时间点。
import numpy as np
def pre_process(x, fixed_len = 1000): # x.shape -> (100, 1000, 1)

    if x.shape[1] >= fixed_len:
       return x[:,:fixed_len,:]
    else:
       z_ph = np.zeros((x.shape[0], fixed_len, x.shape[2]))
       z_ph[:,:x.shape[1],:] = x
       return z_ph

X_batches = pre_process(X_batches, YOU_CHOOSE_THIS_LENGTH) # based on the length of your data
X_test = pre_process(X_test, YOU_CHOOSE_THIS_LENGTH)
© www.soinside.com 2019 - 2024. All rights reserved.