[英]Error: AssertionError: Could not compute output Tensor(“dense_2/truediv:0”, shape=(None, None, 1), dtype=float32)
I am trying to implement an LSTM with input (123,45,4) and output (123,45,1) with a sequence of 4 integers as the input and a single number as the output.我正在尝试使用输入 (123,45,4) 和输出 (123,45,1) 来实现一个 LSTM,其中输入为 4 个整数,输出为单个数字。 I am using Mac OS, Google Colab, and TF version 2.3.0.
我使用的是 Mac OS、Google Colab 和 TF 2.3.0 版。
Here is my model:这是我的模型:
def define_models(n_input, n_output, n_units):
# define training encoder
encoder_inputs = Input(shape=(None, n_input))
encoder = LSTM(n_units, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
# define training decoder
decoder_inputs = Input(shape=(None, n_output))
decoder_lstm = LSTM(n_units, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(n_output, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)
# define inference decoder
decoder_state_input_h = Input(shape=(n_units,))
decoder_state_input_c = Input(shape=(n_units,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
# return all models
return model, encoder_model, decoder_model
When I try running the code: model.fit(x_train, y_train, epochs = 50) I get the error: AssertionError: Could not compute output Tensor("dense_2/truediv:0", shape=(None, None, 1), dtype=float32) .当我尝试运行代码时:model.fit(x_train, y_train, epochs = 50) 我得到错误: AssertionError: 无法计算输出张量("dense_2/truediv:0", shape=(None, None, 1), dtype=float32) 。 Does anyone know how to fix this?
有谁知道如何解决这一问题?
Here is the code to reproduce the issue:这是重现问题的代码:
Load Data:加载数据:
with open("training_data_input.txt") as fopen:
with open("training_data_output.txt") as fopen2:
for line in fopen:
myList = line.strip().split()
myList[0] = myList[0].replace("[","")
if myList[0] == "":
myList = myList[1:]
if "][" in myList[3]:
j = 0
print(myList[3])
myList[3] = myList[3].replace(']][[',"")
if len(myList[3]) > 3:
myList[3] = (myList[3][:3])
myList = myList[:4]
myList[len(myList)-1] = myList[len(myList)-1].replace("]","")
x = np.empty((154,45,4),dtype=np.float32)
i = 0
j = 0
if j >=45:
j = 0
print(myList)
x[i][j] = myList
i+=1
j+=1
for line in fopen2:
myList = line.strip().split()
x_out = np.empty((154,45,1), dtype=np.float32)
myList[0] = myList[0].replace("[","")
if myList[0] == "":
myList = myList[1:]
if "][" in myList[0]:
j = 0
myList[0] = myList[0].replace(']][[',"")
if len(myList[0]) > 3:
myList[0] = (myList[0][:2])
myList = myList[:1]
myList[len(myList)-1] = myList[len(myList)-1].replace("]","")
i = 0
j = 0
if j >=45:
j = 0
x_out[i][j] = myList
i+=1
print(x.shape)
print(x_out.shape)
Train Model:列车型号:
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, x_out, test_size = 0.2, random_state = 4)
print(x_train.shape)
print(y_train.shape)
model.fit(x_train, y_train, epochs = 50)
The input data: training_data_input.txt training_data_output.txt输入数据:training_data_input.txt training_data_output.txt
在model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
您指定 2 个输入,而在 fit 中您只传递 1 个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.