简体   繁体   中英

Simple Neural Network

I have inputs and outputs ( XNOR gate) when I want to train them I'm getting an error.I just wanted to start from the basics but.. 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)
history




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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM