I have inputs and outputs ( XNOR gate) when I want to train them I'm getting an error. Here is the code:
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)
Error:
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)
CodePudding user response:
Your input_shape
is incorrect. Since training_inputs
has the shape (8, 3)
, which means 8 samples with 3 features for each sample, your model should look like this:
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)