在LSTM模型中获取值错误

时间:2019-11-24 13:10:26

标签: python sequence lstm recurrent-neural-network keras-layer

我正在训练一个序列到序列模型,其中训练和目标集的尺寸相等。我有以下代码

import numpy as np
import pandas as pd
from keras.layers import Dense,RNN,LSTM,Activation,Dropout
from keras.models import Sequential
import tensorflow as tf
# Let us do for input data
data = pd.read_csv('E:\Final Work\yout1.csv') 
#print(data)
X = data[:16255]
X = np.array(X)
print(X[-1])
X1 = np.append(X,[1554.671208])
#print(X1)
print(len(X1))
XX = X1.tolist()
n = 16256
length = 254
samples = list()
for i in range(0,n,length):
sample = XX[i:i+length]
samples.append(sample)
#print(len(samples))
#print(samples)
traindata = np.array(samples)
traindata = traindata.reshape((len(samples), length, 1))
print(traindata.shape)
# Now do it for target data
data2 = pd.read_csv('E:\Final Work\counterforce.csv') 
#print(data)
X2 = data[:16255]
X2 = np.array(X2)
print(X2[-1])
X12 = np.append(X2,[1554.671208])
#print(X1)
#print(X12.shape)
XX2 = X12.tolist()
n = 16256
length = 254
target = list()
for i in range(0,n,length):
    sample2 = XX2[i:i+length]
    target.append(sample2)
#print(len(target))
#print(target)
traintarget = np.array(target)
traintarget = traintarget.reshape((len(target), length, 1))
print(traintarget.shape)
model = Sequential()
model.add(LSTM(3,input_shape=(254,1),return_sequences=True))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(LSTM(1))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(traindata,traintarget,batch_size=1,epochs=10,validation_split=0.2)
p = model.predict(traindata)
print(p)

但是,获取价值错误 ValueError:检查目标时出错:预期activation_24具有2个维,但数组的形状为(64,254,1) 在第57行上model.fit(traindata,traintarget,batch_size = 1,epochs = 10,validation_split = 0.2)

我想这与batch_size有关。但不确定

0 个答案:

没有答案