为了账号安全,请及时绑定邮箱和手机立即绑定

警告:tensorflow:模型是使用输入 Tensor 的形状 (20, 37, 42) 构建

警告:tensorflow:模型是使用输入 Tensor 的形状 (20, 37, 42) 构建

蓝山帝景 2023-07-11 14:34:56
警告:tensorflow:模型是使用形状 (20, 37, 42) 为输入 Tensor("input_5:0", shape=(20, 37, 42), dtype=float32) 构造的,但在不兼容的输入上调用它形状(无,37)。你好!深度学习菜鸟在这里...我在使用 LSTM 层时遇到问题。输入是一个长度为 37 的 float 数组,其中包含 2 个浮点数和一个转换为 float 的长度为 35 的 one-hot 数组。输出是一个长度为 19 的数组,其中包含 0 和 1。正如标题所示,我在重塑输入数据以适应模型时遇到了麻烦,而且我什至不确定什么输入维度会被视为“兼容”import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import layersimport randominputs, outputs = [], []for x in range(10000):    tempi, tempo = [], []    tempi.append(random.random() - 0.5)    tempi.append(random.random() - 0.5)    for x2 in range(35):        if random.random() > 0.5:            tempi.append(1.)        else:            tempi.append(0.)    for x2 in range(19):        if random.random() > 0.5:            tempo.append(1.)        else:            tempo.append(0.)    inputs.append(tempi)    outputs.append(tempo)batch = 20timesteps = 42training_units = 0.85cutting_point_i = int(len(inputs)*training_units)cutting_point_o = int(len(outputs)*training_units)x_train, x_test = np.asarray(inputs[:cutting_point_i]), np.asarray(inputs[cutting_point_i:])y_train, y_test = np.asarray(outputs[:cutting_point_o]), np.asarray(outputs[cutting_point_o:])input_layer = keras.Input(shape=(37,timesteps),batch_size=batch)dense = layers.LSTM(150, activation="sigmoid", return_sequences=True)x = dense(input_layer)hidden_layer_2 = layers.LSTM(150, activation="sigmoid", return_sequences=True)(x)output_layer = layers.Dense(10, activation="softmax")(hidden_layer_2)model = keras.Model(inputs=input_layer, outputs=output_layer, name="my_model"
查看完整描述

2 回答

?
郎朗坤

TA贡献1921条经验 获得超9个赞

这里有几个问题。

  • 您的输入没有时间步长,您需要输入形状(n, time steps, features)

  • 在 中input_shape,时间步长维度首先出现,而不是最后出现

  • 您的最后一个 LSTM 层返回序列,因此您无法将其与 0 和 1 进行比较

我做了什么:

  • 我向您的数据添加了时间步 (7)

  • 我排列了尺寸input_shape

  • 我定了最后的结局return_sequences=False

使用生成的数据完全修复示例:

import numpy as np

from tensorflow import keras

from tensorflow.keras import layers


batch = 20

n_samples = 1000

timesteps = 7

features = 10


x_train = np.random.rand(n_samples, timesteps, features)

y_train = keras.utils.to_categorical(np.random.randint(0, 10, n_samples))


input_layer = keras.Input(shape=(timesteps, features),batch_size=batch)

dense = layers.LSTM(16, activation="sigmoid", return_sequences=True)(input_layer)

hidden_layer_2 = layers.LSTM(16, activation="sigmoid", return_sequences=False)(dense)

output_layer = layers.Dense(10, activation="softmax")(hidden_layer_2)

model = keras.Model(inputs=input_layer, outputs=output_layer, name="my_model")


model.compile(loss='categorical_crossentropy', optimizer='adam')


history = model.fit(x_train, y_train)

Train on 1000 samples

  20/1000 [..............................] - ETA: 2:50 - loss: 2.5145

 200/1000 [=====>........................] - ETA: 14s - loss: 2.3934 

 380/1000 [==========>...................] - ETA: 5s - loss: 2.3647 

 560/1000 [===============>..............] - ETA: 2s - loss: 2.3549

 740/1000 [=====================>........] - ETA: 1s - loss: 2.3395

 900/1000 [==========================>...] - ETA: 0s - loss: 2.3363

1000/1000 [==============================] - 4s 4ms/sample - loss: 2.3353


查看完整回答
反对 回复 2023-07-11
?
UYOU

TA贡献1878条经验 获得超4个赞

您的模型的正确输入是 (20, 37, 42)。注意:这里20是您明确指定的batch_size。


代码:


import numpy as np

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras import layers


batch = 20

timesteps = 42

training_units = 0.85


x1 = tf.constant(np.random.randint(50, size =(1000,37, 42)), dtype = tf.float32)

y1 = tf.constant(np.random.randint(10, size =(1000,)), dtype = tf.int32)

 


input_layer = keras.Input(shape=(37,timesteps),batch_size=batch)

dense = layers.LSTM(150, activation="sigmoid", return_sequences=True)

x = dense(input_layer)

hidden_layer_2 = layers.LSTM(150, activation="sigmoid", return_sequences=True)(x)

hidden_layer_3 = layers.Flatten()(hidden_layer_2)

output_layer = layers.Dense(10, activation="softmax")(hidden_layer_3)

model = keras.Model(inputs=input_layer, outputs=output_layer, name="my_model")


model.compile(optimizer='adam',

                  loss='sparse_categorical_crossentropy',

                  metrics=['accuracy'])

tf.keras.utils.plot_model(model, 'my_first_model.png', show_shapes=True)

模型架构:

//img1.sycdn.imooc.com//64acf859000192ef03890516.jpg

您可以清楚地看到输入大小。


要运行的代码:


model.fit(x = x1, y = y1, batch_size = batch, epochs = 10)

注意:无论您指定了何种batch_size,都必须在 model.fit() 命令中指定相同的batch_size。


输出:


Epoch 1/10

50/50 [==============================] - 4s 89ms/step - loss: 2.3288 - accuracy: 0.0920

Epoch 2/10

50/50 [==============================] - 5s 91ms/step - loss: 2.3154 - accuracy: 0.1050

Epoch 3/10

50/50 [==============================] - 5s 101ms/step - loss: 2.3114 - accuracy: 0.0900

Epoch 4/10

50/50 [==============================] - 5s 101ms/step - loss: 2.3036 - accuracy: 0.1060

Epoch 5/10

50/50 [==============================] - 5s 99ms/step - loss: 2.2998 - accuracy: 0.1000

Epoch 6/10

50/50 [==============================] - 4s 89ms/step - loss: 2.2986 - accuracy: 0.1170

Epoch 7/10

50/50 [==============================] - 4s 84ms/step - loss: 2.2981 - accuracy: 0.1300

Epoch 8/10

50/50 [==============================] - 5s 103ms/step - loss: 2.2950 - accuracy: 0.1290

Epoch 9/10

50/50 [==============================] - 5s 106ms/step - loss: 2.2960 - accuracy: 0.1210

Epoch 10/10

50/50 [==============================] - 5s 97ms/step - loss: 2.2874 - accuracy: 0.1210


查看完整回答
反对 回复 2023-07-11
  • 2 回答
  • 0 关注
  • 153 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信