【问题标题】:Simple Neural Network简单的神经网络
【发布时间】:2022-01-08 15:34:27
【问题描述】:

当我想训练它们时,我有输入和输出(XNOR 门),但我遇到了错误。代码如下:

import tensorflow as tf
import numpy as np

training_inputs = np.array([[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]],dtype=float)
training_outputs =np.array([1,0,0,1,0,1,1,0],dtype=float)

model = tf.keras.Sequential([
  tf.keras.layers.Dense(units=1, input_shape=[1])
])


model.compile(loss='mean_squared_error',
              optimizer=tf.keras.optimizers.Adam(0.1))

history = model.fit(training_inputs, training_outputs , epochs=500, verbose=False)

错误:

ValueError: Exception encountered when calling layer "sequential_14" (type Sequential).
    
    Input 0 of layer "dense_14" is incompatible with the layer: expected axis -1of input shape to have value 1, but received input with shape (None, 2)

【问题讨论】:

    标签: python tensorflow machine-learning deep-learning neural-network


    【解决方案1】:

    您的input_shape 不正确。由于training_inputs 的形状为(8, 3),这意味着每个样本有8 个样本,每个样本有3 个特征,您的模型应该如下所示:

    import tensorflow as tf
    import numpy as np
    
    training_inputs = np.array([[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]],dtype=float)
    training_outputs =np.array([1,0,0,1,0,1,1,0],dtype=float)
    
    model = tf.keras.Sequential([
      tf.keras.layers.Dense(units=1, input_shape=(3,))
    ])
    
    
    model.compile(loss='mean_squared_error',
                  optimizer=tf.keras.optimizers.Adam(0.1))
    
    history = model.fit(training_inputs, training_outputs , epochs=500, verbose=False, batch_size=2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2011-06-10
      • 2018-04-10
      • 2017-02-09
      • 2013-03-02
      • 2018-12-23
      • 2018-05-24
      相关资源
      最近更新 更多