0
votes
import tensorflow as tf
from tensorflow import keras
import numpy as np

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

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

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(1,3)),
    keras.layers.Dense(1,activation="sigmoid")
    ])

model.compile(optimizer = "rmsprop",
              loss = "binary_crossentropy",
              metrics = ["accuracy"])

model.fit(training_inputs,training_outputs,epochs=1)

prediction = model.predict(np.array([[1,1,0]]))

print(prediction)

it has these problems

Traceback (most recent call last): File "C:/Users/Αλέξης/Desktop/Youtube/test.py", line 21, in model.fit(training_inputs,training_outputs,epochs=1) File "C:\Users\Αλέξης\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 709, in fit shuffle=shuffle) File "C:\Users\Αλέξης\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training.py", line 2651, in _standardize_user_data exception_prefix='input') File "C:\Users\Αλέξης\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 376, in standardize_input_data 'with shape ' + str(data_shape)) ValueError: Error when checking input: expected flatten_input to have 3 dimensions, but got array with shape (4, 3)

Can anyone help?

2
Seems like the dimensions of your training input are not matching the ones given by the input_shape - elolelo

2 Answers

0
votes

I delete the square brackets for the training outputs array so that each entry in training inputs has an associated class instead of just the first one having 4

I also deleted the flatten layer and set the input_shape parameter to the dense layer

import tensorflow as tf
from tensorflow import keras
import numpy as np

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

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

model = keras.Sequential([
    keras.layers.Dense(1,activation="sigmoid",input_shape=(3,))
])

model.compile(optimizer = "rmsprop",
          loss = "binary_crossentropy",
          metrics = ["accuracy"])

model.fit(training_inputs,training_outputs,epochs=1)
0
votes

If you check your arrays shapes, you will see the problem

print(training_inputs.shape)
print(training_outputs.shape)

Output:
(4, 3)
(4,)

Your model expects an input array of size (Batch size, 3), and that's ok. But your model outputs an array of size (Batch size, 1), thus your label array must to have the same size. You can easily fix that using np.expand_dims.

Just turn

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

Into

training_outputs = np.expand_dims(
    np.array([0,1,1,0]),
    axis = 1
)