InvalidArgumentError:矩阵大小不兼容:In [0]:[30,8],In [1]:[4,30] [[{{node lstm_1 / while / MatMul_2}}]]

时间:2019-12-14 16:24:41

标签: python keras lstm invalid-argument

我遇到了一个错误,而这只能靠我自己解决。 我正在尝试使用(,8)输入维和(,1)输出维构建LSTM。 提供的数据具有(,9)维,并将其分为两部分进行训练,答案和问题。您能帮我解决这个问题吗?

这是我的完整错误消息。

Train on 12501 samples, validate on 5373 samples
Epoch 1/50
Traceback (most recent call last):

  File "<ipython-input-59-36fcdafa7a4b>", line 1, in <module>
    runfile('C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN_test.py', wdir='C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise')

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
    execfile(filename, namespace)

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/SONSANGWOO/Desktop/Euroaquae/The_third_semester_at_BCN/ANN/Exercise/TimeSeriespy_RNN_test.py", line 89, in <module>
    hist = model.fit(np.array(trainX), np.array(trainY), epochs=iterations, batch_size=30, validation_data=(np.array(testX), np.array(testY)))

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\keras\engine\training.py", line 1039, in fit
    validation_steps=validation_steps)

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 199, in fit_loop
    outs = f(ins_batch)

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
    return self._call(inputs)

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
    fetched = self._callable_fn(*array_vals)

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)

  File "C:\Users\SONSANGWOO\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))

InvalidArgumentError: Matrix size-incompatible: In[0]: [30,8], In[1]: [4,30]
     [[{{node lstm_1/while/MatMul_2}}]]

这是我的ANN代码。 输入数据是下面的链接。 [天气] https://drive.google.com/file/d/1alc7OEONmdL-RgYLBrj3vcZ2fuiaIPu7/view?usp=sharing

import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
#from sklearn import preprocessing
from keras.models import Sequential
from keras.layers import Dense, LSTM, Dropout, Flatten
from sklearn.preprocessing import MinMaxScaler

#random_weight_generators
tf.reset_default_graph()
tf.set_random_seed(777)  # reproducibility


# train Parameters
seq_length = 36
data_dim = 8
hidden_dim = 40
output_dim = 1
learning_rate = 0.01
iterations = 50
last_column = 8 #last column of input
percentage = 0.7 #percentage of validation and training data



# Open, High, Low, Volume, Close
xy = np.loadtxt('Busan_no_date.txt', dtype='double', delimiter=' ', skiprows=1)
xy = xy[::-1]  # reverse order (chronically ordered)

# Scale each
#xy= MinMaxScaler(xy)
MinMaxScaler = MinMaxScaler()
xy = MinMaxScaler.fit_transform(xy)

# train/test split
train_size = int(len(xy) * percentage)
train_set =  xy[0:train_size]
pred_set = xy[0:6000]
np.random.shuffle(train_set)
test_set = xy[train_size - seq_length:] # Index from [train_size - seq_length] to utilize past sequence



# Test, Validation data separation, int -> list
def input_seperation(data):
    inputX=data[:,:last_column]
    inputY=data[:,[-1]]   
    return inputX, inputY

input_trainX, input_trainY = input_seperation(train_set)
input_testX, input_testY = input_seperation(test_set)
input_predX, input_predY = input_seperation(pred_set)


# build datasets(window)
def build_dataset(queX, ansY, seq_length):
    dataX = []
    dataY = []
    for i in range(0, len(queX) - int(seq_length)):
        _x = queX[i:i + seq_length, :]
        _y = ansY[i + seq_length, :]
        dataX.append(_x)
        dataY.append(_y)
    return dataX, dataY

trainX, trainY = build_dataset(input_trainX, input_trainY, seq_length)
testX, testY = build_dataset(input_testX, input_testY ,seq_length)
predX, predY = build_dataset(input_predX, input_predY, seq_length)



#build model
model = Sequential()
model.add(LSTM(hidden_dim, activation='softmax', input_shape=(int(seq_length),int(data_dim))))
model.add(Dropout(0.3))
model.add(Dense(1))

#set the model's learning
model.compile(loss='mean_squared_error', optimizer='adam')
#training model
hist = model.fit(np.array(trainX), np.array(trainY), epochs=iterations, batch_size=30, validation_data=(np.array(testX), np.array(testY)))

#See the training process
plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.ylim(0.0, 100)
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

#Evaluating the model
trainScore = model.evaluate(np.array(trainX), np.array(trainY), verbose=0)
print('Train Score: ', trainScore)
valScore = model.evaluate(np.array(testX), np.array(testY), verbose=0)
print('Validataion Score: ', valScore)
testScore = model.evaluate(np.array(testX), np.array(testY), verbose=0)
print('Test Score: ', testScore)
#Validatin LSTM
val_Y = model.predict(np.array(testX), batch_size=1)
#val_Y=np.exp(val_Y)
#Asking to LSTM
pred_Y = model.predict(np.array(predX), batch_size=1)
#pred_Y=np.exp(pred_Y)    

#plot the prediction
a_axis = np.arange(0, len(trainY))
b_axis = np.arange(len(trainY), len(trainY) + len(testY))
c_axis = np.arange(len(trainY) + len(testY), len(trainY) + len(testY) + len(pred_Y))
plt.figure(figsize=(10,6))
plt.plot(a_axis, np.array(trainY), 'o-', color='b', alpha=0.5, linewidth=0.5, label='training')
plt.plot(b_axis, np.array(val_Y), 'o-', color='red', linewidth=0.5, label='Validated')
plt.plot(b_axis, np.array(testY), 'o-', color='b', alpha=0.5, linewidth=0.5, label='training')
plt.plot(c_axis, np.array(pred_Y), 'o-', color='green', linewidth=0.5, label='predicted')
plt.legend()
plt.show()

0 个答案:

没有答案