【发布时间】:2019-08-25 19:51:09
【问题描述】:
我正在尝试使用 tensorflow.keras 训练神经网络,但我不明白如何使用 numpy 列表数组(在 python3 中)训练它。
我试图改变图层的输入形状,但我不太明白它是如何工作的。
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Create the array of data
train_data = [[1.0,2.0,3.0],[4.0,5.0,6.0]]
train_data_np = np.asarray(train_data)
train_label = [[1,2,3],[4,5,6]]
train_label_np = np.asarray(train_data)
### Build the model
model = keras.Sequential([
keras.layers.Dense(3,input_shape =(3,2)),
keras.layers.Dense(3,activation=tf.nn.sigmoid)
])
model.compile(optimizer='sgd',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
#Train the model
model.fit(train_data_np,train_label_np,epochs=10)
当调用 model.fit 时,错误是“检查输入时出错:预期的 dense_input 具有 3 个维度,但得到的数组的形状为 (2, 3)”。
【问题讨论】:
-
要么对二维数组使用卷积层(形状 3,2),要么必须展平数组,因此形状为 3,用于密集层。而且我不知道您的最后一层具有 sigmoid 函数如何适合 2、3、4、5 甚至 6。您的火车标签将是 [[1,0,0], [0,1,0]]例如...尝试阅读an introduction to Keras
标签: python tensorflow keras