简体   繁体   English

错误:断言错误:无法计算输出张量(“dense_2/truediv:0”,形状=(无,无,1),dtype=float32)

[英]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.

相关问题 AssertionError:无法计算 output 张量(“dense_17/Sigmoid:0”,shape=(None,1),dtype=float32) - AssertionError: Could not compute output Tensor(“dense_17/Sigmoid:0”, shape=(None, 1), dtype=float32) AssertionError:无法计算 output 张量(“softmax_layer/Identity:0”,shape=(None, 27, 8870),dtype=float32) - AssertionError: Could not compute output Tensor(“softmax_layer/Identity:0”, shape=(None, 27, 8870), dtype=float32) python - ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 43), dtype=float32) 不是这个图的一个元素 - python - ValueError: Tensor Tensor("dense_2/Softmax:0", shape=(?, 43), dtype=float32) is not an element of this graph Tensor(“dense_2/Softmax:0”, shape=(?, 10), dtype=float32) 不是该图的元素 - Tensor(“dense_2/Softmax:0”, shape=(?, 10), dtype=float32) is not an element of this graph ValueError:收到呼叫 arguments: • inputs=tf.Tensor(shape=(None, 1), dtype=float32) • training=None - ValueError : Call arguments received: • inputs=tf.Tensor(shape=(None, 1), dtype=float32) • training=None 图断开连接:无法在“input_1”层获取张量 Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 299, 299, 3), dtype=float32) at layer "input_1" 图表断开:无法在“input_5”层获取张量 Tensor(“input_5:0”, shape=(None, None, None, 128), dtype=float32) 的值 - Graph disconnected: cannot obtain value for tensor Tensor(“input_5:0”, shape=(None, None, None, 128), dtype=float32) at layer “input_5” 类型错误:添加的图层必须是类图层的实例。 找到:Tensor("concatenate_6/concat:0", shape=(None, 4608), dtype=float32) - TypeError: The added layer must be an instance of class Layer. Found: Tensor("concatenate_6/concat:0", shape=(None, 4608), dtype=float32) ValueError:模型输出“Tensor(“activation_1/Identity:0”, shape=(?, 3), dtype=float32)”的形状无效 - ValueError: Model output "Tensor("activation_1/Identity:0", shape=(?, 3), dtype=float32)" has invalid shape TypeError:添加的层必须是 class 层的实例。 找到:Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python - TypeError: The added layer must be an instance of class Layer. Found: Tensor(“input_1:0”, shape=(None, 64, 64, 3), dtype=float32) -Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM