【问题标题】:How does a Neural Network remember its training?神经网络如何记住它的训练?
【发布时间】:2021-09-07 19:02:00
【问题描述】:

following source code 用于在 Python 中训练神经网络:

# Visualize training history
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
import numpy
# load pima indians dataset
dataset = numpy.loadtxt("file.txt", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
history = model.fit(X, Y, validation_split=0.33, epochs=150, batch_size=10, verbose=0)
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()

完成此训练后,神经网络会将其记忆保存在哪里?

如果没有,我该如何实现?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您可以像 Kerrim 所说的那样保存整个模型。此外,您可以使用 ``` 保存神经网络的权重 model.save_weights

    https://www.tensorflow.org/guide/keras/save_and_serialize
    

    【讨论】:

      【解决方案2】:

      您可以使用以下方式保存模型:

      model.save('saved_model/my_model')
      

      并加载模型:

      import tensorflow as tf
      new_model = tf.keras.models.load_model('saved_model/my_model')
      

      https://www.tensorflow.org/tutorials/keras/save_and_load#save_the_entire_model

      【讨论】:

        猜你喜欢
        • 2011-04-07
        • 1970-01-01
        • 2010-11-20
        • 2019-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-30
        相关资源
        最近更新 更多