Keras,顺序神经网络模型
Keras, Sequential Neural Network Model
这是 Keras 模型的代码,它给出了类型错误
model=keras.Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0,5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0,5))
model.add(Dense(len(train_y[0]), activation='softmax'))
sgd= SGD(lr=0.01, decay=1e-6, momentum=0.9,nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer= sgd, metrics=
['accuracy'])
model.fit(np.array(train_x),np.array(train_y),epochs=200,batch_size=5,
verbose=1)
model.save("chatbot.model")
print("training is done")
here i 'm creating a chat bot using Keras Sequential Model and encountered
the TypeError which shows on the Training the Neural Network, exact on model.fit line
注意:
意图是我以字典格式给出的消息,这是示例
{"intents":[{"tag": "welcome", "patterns":["Hi","Hello"],"responses":["Hello","Hi"]}
我已经导入了 nltk、nltk.stem-WordNetLemmatizer、numpy、pickle、random、tensorflow、keras、Keras.Model-Sequential、Keras.Layers-Dense、Activation 和 Dropout。 Keras.Optimizers-新元
> TypeError: in user code: /usr/local/lib/python3.6/dist-
>
> packages/tensorflow/python/keras/engine/training.py:805 train_function
> * return step_function(self, iterator) /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/training.py:795
> step_function ** outputs = model.distribute_strategy.run(run_step,
> args=(data,))
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/distribute/distribute_lib.py:1259
> run return self._extended.call_for_each_replica(fn, args=args,
> kwargs=kwargs)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/distribute/distribute_lib.py:2730
> call_for_each_replica return self._call_for_each_replica(fn, args,
> kwargs)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/distribute/distribute_lib.py:3417
> _call_for_each_replica return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/training.py:788
> run_step ** outputs = model.train_step(data)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/training.py:754
> train_step y_pred = self(x, training=True)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/base_layer.py:1012
> __call__ outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/sequential.py:375
> call return super(Sequential, self).call(inputs, training=training,
> mask=mask)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/functional.py:425
> call inputs, training=training, mask=mask)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/functional.py:560
> _run_internal_graph outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012
> __call__ outputs = call_fn(inputs, *args, **kwargs) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:231
> call lambda: array_ops.identity(inputs))
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/control_flow_util.py:115
> smart_cond pred, true_fn=true_fn, false_fn=false_fn, name=name)
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/smart_cond.py:54
> smart_cond return true_fn()
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:226
> dropped_inputs noise_shape=self._get_noise_shape(inputs),
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:215
> _get_noise_shape for i, value in enumerate(self.noise_shape):
>
> TypeError: 'int' object is not iterable
我的建议是这些行是错误的:
model.add(Dropout(0.5)) # replace comma by dot
@Andrey 是正确的。还有其他一些小问题,但这是错误的原因。
这里是固定代码-
from tensorflow import keras
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import SGD
import numpy as np
train_x = np.random.random((100,8))
train_y = np.random.random((100,4))
model=keras.Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))
sgd= SGD(lr=0.01, decay=1e-6, momentum=0.9,nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer= sgd, metrics=
['accuracy'])
model.fit(np.array(train_x),np.array(train_y),epochs=3,batch_size=5,
verbose=1)
print("training is done")
print(model.summary())
Epoch 1/3
20/20 [==============================] - 0s 820us/step - loss: 2.6962 - accuracy: 0.0820
Epoch 2/3
20/20 [==============================] - 0s 807us/step - loss: 2.9444 - accuracy: 0.3395
Epoch 3/3
20/20 [==============================] - 0s 741us/step - loss: 336196611196720054272.0000 - accuracy: 0.2951
training is done
Model: "sequential_24"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_66 (Dense) (None, 128) 1152
_________________________________________________________________
dropout_8 (Dropout) (None, 128) 0
_________________________________________________________________
dense_67 (Dense) (None, 64) 8256
_________________________________________________________________
dropout_9 (Dropout) (None, 64) 0
_________________________________________________________________
dense_68 (Dense) (None, 4) 260
=================================================================
Total params: 9,668
Trainable params: 9,668
Non-trainable params: 0
_________________________________________________________________
这是 Keras 模型的代码,它给出了类型错误
model=keras.Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0,5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0,5))
model.add(Dense(len(train_y[0]), activation='softmax'))
sgd= SGD(lr=0.01, decay=1e-6, momentum=0.9,nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer= sgd, metrics=
['accuracy'])
model.fit(np.array(train_x),np.array(train_y),epochs=200,batch_size=5,
verbose=1)
model.save("chatbot.model")
print("training is done")
here i 'm creating a chat bot using Keras Sequential Model and encountered
the TypeError which shows on the Training the Neural Network, exact on model.fit line
注意:
意图是我以字典格式给出的消息,这是示例
{"intents":[{"tag": "welcome", "patterns":["Hi","Hello"],"responses":["Hello","Hi"]}
我已经导入了 nltk、nltk.stem-WordNetLemmatizer、numpy、pickle、random、tensorflow、keras、Keras.Model-Sequential、Keras.Layers-Dense、Activation 和 Dropout。 Keras.Optimizers-新元
> TypeError: in user code: /usr/local/lib/python3.6/dist-
>
> packages/tensorflow/python/keras/engine/training.py:805 train_function
> * return step_function(self, iterator) /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/training.py:795
> step_function ** outputs = model.distribute_strategy.run(run_step,
> args=(data,))
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/distribute/distribute_lib.py:1259
> run return self._extended.call_for_each_replica(fn, args=args,
> kwargs=kwargs)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/distribute/distribute_lib.py:2730
> call_for_each_replica return self._call_for_each_replica(fn, args,
> kwargs)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/distribute/distribute_lib.py:3417
> _call_for_each_replica return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/training.py:788
> run_step ** outputs = model.train_step(data)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/training.py:754
> train_step y_pred = self(x, training=True)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/base_layer.py:1012
> __call__ outputs = call_fn(inputs, *args, **kwargs)
/usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/sequential.py:375
> call return super(Sequential, self).call(inputs, training=training,
> mask=mask)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/functional.py:425
> call inputs, training=training, mask=mask)
> /usr/local/lib/python3.6/dist-
packages/tensorflow/python/keras/engine/functional.py:560
> _run_internal_graph outputs = node.layer(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012
> __call__ outputs = call_fn(inputs, *args, **kwargs) /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:231
> call lambda: array_ops.identity(inputs))
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/control_flow_util.py:115
> smart_cond pred, true_fn=true_fn, false_fn=false_fn, name=name)
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/smart_cond.py:54
> smart_cond return true_fn()
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:226
> dropped_inputs noise_shape=self._get_noise_shape(inputs),
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/core.py:215
> _get_noise_shape for i, value in enumerate(self.noise_shape):
>
> TypeError: 'int' object is not iterable
我的建议是这些行是错误的:
model.add(Dropout(0.5)) # replace comma by dot
@Andrey 是正确的。还有其他一些小问题,但这是错误的原因。
这里是固定代码-
from tensorflow import keras
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.optimizers import SGD
import numpy as np
train_x = np.random.random((100,8))
train_y = np.random.random((100,4))
model=keras.Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))
sgd= SGD(lr=0.01, decay=1e-6, momentum=0.9,nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer= sgd, metrics=
['accuracy'])
model.fit(np.array(train_x),np.array(train_y),epochs=3,batch_size=5,
verbose=1)
print("training is done")
print(model.summary())
Epoch 1/3
20/20 [==============================] - 0s 820us/step - loss: 2.6962 - accuracy: 0.0820
Epoch 2/3
20/20 [==============================] - 0s 807us/step - loss: 2.9444 - accuracy: 0.3395
Epoch 3/3
20/20 [==============================] - 0s 741us/step - loss: 336196611196720054272.0000 - accuracy: 0.2951
training is done
Model: "sequential_24"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_66 (Dense) (None, 128) 1152
_________________________________________________________________
dropout_8 (Dropout) (None, 128) 0
_________________________________________________________________
dense_67 (Dense) (None, 64) 8256
_________________________________________________________________
dropout_9 (Dropout) (None, 64) 0
_________________________________________________________________
dense_68 (Dense) (None, 4) 260
=================================================================
Total params: 9,668
Trainable params: 9,668
Non-trainable params: 0
_________________________________________________________________