在 Tensorflow 中构建 2D 到 2D 神经网络模型——无效的形状
Building a 2D to 2D Neural Network Model in Tensorflow -- Invalid Shapes
我想在 Tensorflow2 中设计一个将二维值映射到其他二维值的神经网络。我不知道如何初始化我的模型来执行此操作而不给我尺寸错误。
import numpy
import tensorflow as tf
def euclidean_distance_loss(y_true, y_pred):
return numpy.sqrt(sum((y_true - y_pred)**2.0))
#===Make Data====#
x_train = numpy.asarray([ [1, 2], [3, 1], [2, 2] ])
x_test = numpy.asarray([ [10, 1], [2, 0], [5, 1] ])
y_train = numpy.asarray([ [3, 8], [2, 7], [3, 3] ])
y_test = numpy.asarray([ [1, 0], [0, 1], [4, 9] ])
#===Make Model===#
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128,input_shape=(1, x_train_points.shape[1]), activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(5, activation='relu')
])
#===Run Model===#
model.compile(optimizer='adam', loss=euclidean_distance_loss, metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
当我尝试 运行 这个时,我得到:
ValueError: Dimensions must be equal, but are 2 and 5 for '{{node euclidean_distance_lowss/sub}} = Sub[T=DT_Float](IteratorGetNext:1, sequential/dense_1/Relu)' with input schapes: [?,2], [?,5].
我应该如何设置这个神经网络才能接收这种类型的二维数据?抱歉这个基本问题——我刚刚开始使用 Tensorflow2!
编辑:给定一个二维向量作为输入,我希望模型输出另一个二维向量。
模型的最后一层正在为每个示例定义一个形状 [5]
输出张量:
tf.keras.layers.Dense(5, activation='relu')
但是标签(y_train
和 y_test
)每个示例的形状为 [2]
:
y_train = numpy.asarray([ [3, 8], [2, 7], [3, 3] ])
y_test = numpy.asarray([ [1, 0], [0, 1], [4, 9] ])
正在尝试将最后一层更改为有 2 个单元。
我想在 Tensorflow2 中设计一个将二维值映射到其他二维值的神经网络。我不知道如何初始化我的模型来执行此操作而不给我尺寸错误。
import numpy
import tensorflow as tf
def euclidean_distance_loss(y_true, y_pred):
return numpy.sqrt(sum((y_true - y_pred)**2.0))
#===Make Data====#
x_train = numpy.asarray([ [1, 2], [3, 1], [2, 2] ])
x_test = numpy.asarray([ [10, 1], [2, 0], [5, 1] ])
y_train = numpy.asarray([ [3, 8], [2, 7], [3, 3] ])
y_test = numpy.asarray([ [1, 0], [0, 1], [4, 9] ])
#===Make Model===#
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(128,input_shape=(1, x_train_points.shape[1]), activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(5, activation='relu')
])
#===Run Model===#
model.compile(optimizer='adam', loss=euclidean_distance_loss, metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
当我尝试 运行 这个时,我得到:
ValueError: Dimensions must be equal, but are 2 and 5 for '{{node euclidean_distance_lowss/sub}} = Sub[T=DT_Float](IteratorGetNext:1, sequential/dense_1/Relu)' with input schapes: [?,2], [?,5].
我应该如何设置这个神经网络才能接收这种类型的二维数据?抱歉这个基本问题——我刚刚开始使用 Tensorflow2!
编辑:给定一个二维向量作为输入,我希望模型输出另一个二维向量。
模型的最后一层正在为每个示例定义一个形状 [5]
输出张量:
tf.keras.layers.Dense(5, activation='relu')
但是标签(y_train
和 y_test
)每个示例的形状为 [2]
:
y_train = numpy.asarray([ [3, 8], [2, 7], [3, 3] ])
y_test = numpy.asarray([ [1, 0], [0, 1], [4, 9] ])
正在尝试将最后一层更改为有 2 个单元。