When I tried to use model.fit in Tensorflow and add options it keeps showing me this error for batchSize and epochs, I have checked the docs and it is the same as I did so what could be the error
model = Sequential([
Dense(units=16,input_shape=(1,), activation='relu'), # layer 1
Dense(units=32, activation='relu'), # layer 2
Dense(units=2, activation='softmax'), # layer 3 fully connected layer
])
model.compile(optimizer=Adam(learning_rate=0.0001), loss='sparse_categorical_crossentropy', metrics=['aaccracy'])
model.fit(scaled_train_samples, train_labels, {
batchSize: 4,
epochs: 3
})
I tried to use it this way and it show me this error also
model.fit(scaled_train_samples, train_labels, batch_size=4,epochs=3)
scaled_train_samples
[
[0.22093023]
[0.6627907]
[0.44186047]
[0.40697674]
[0.97674419]
[0.04651163]
[0.19767442]
[0.61627907]
[0.03488372]
[0.44186047]
[0.43023256]
[0.8255814]
[0.48837209]
[0.20930233]
[0.46511628]
[0.81395349]
[0.15116279]
[0.18604651]
[0.43023256]
[0.61627907]
]
train_labels
[
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
]
CodePudding user response:
Try this:
import tensorflow as tf
import numpy as np
scaled_train_samples = [
[0.44186047],[0.40697674],[0.97674419],[0.04651163],[0.19767442],
[0.61627907],[0.03488372],[0.44186047],[0.43023256],[0.8255814],
[0.48837209],[0.20930233],[0.46511628],[0.81395349],[0.15116279],
[0.18604651],[0.43023256],[0.61627907]]
train_labels = [1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]
print(np.asarray(scaled_train_samples).shape)
# (18, 1)
print(np.asarray(train_labels).shape)
# (18,)
model = tf.keras.Sequential([
tf.keras.layers.Dense(units=16,input_shape=(1,), activation='relu'), # layer 1
tf.keras.layers.Dense(units=32, activation='relu'), # layer 2
tf.keras.layers.Dense(units=2, activation='softmax'), # layer 3 fully connected layer
])
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss='sparse_categorical_crossentropy',
metrics=['acc'])
model.fit(scaled_train_samples, train_labels, batch_size=4, epochs=3)
Epoch 1/3
5/5 [==============================] - 0s 3ms/step - loss: 0.6921 - acc: 0.5000
Epoch 2/3
5/5 [==============================] - 0s 2ms/step - loss: 0.6916 - acc: 0.5000
Epoch 3/3
5/5 [==============================] - 0s 7ms/step - loss: 0.6914 - acc: 0.5556
CodePudding user response:
The NameError: name 'batchSize' is not defined
diagnostic is pretty clear.
Without defining a pair of string constants, you wrote
fit(..., ..., {
batchSize: 4,
epochs: 3
})
You meant either
(1.)
fit(..., ..., {
'batchSize': 4,
'epochs': 3
})
or equivalently
(2.)
fit(..., ..., dict(
batchSize=4,
epochs=3
))