繁体   English   中英

无法在 python 中执行泡菜

[英]Cannot perform pickle in python

    def myModel():
        noOfFilters = 60
        sizeOfFilter1 = (5, 5)
        sizeOfFilter2 = (3, 3)
        sizeOfPool = (2, 2)
        noOfNodes = 500
    
        model = Sequential()
        model.add((Conv2D(6, sizeOfFilter1, input_shape=(32, 32, 1), activation='relu')))
        model.add(MaxPooling2D(pool_size=sizeOfPool))
        model.add((Conv2D(16, sizeOfFilter1, activation='relu')))
        model.add(MaxPooling2D(pool_size=sizeOfPool))
        model.add(Flatten())
        model.add(Dense(120,activation='relu'))
        model.add(Dense(84, activation='relu'))
        model.add(Dropout(0.4))
        model.add(Dense(noOfClasses, activation='softmax'))
        model.compile(Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
        return model
    model = myModel()
    
    history = model.fit(X_train,y_train,validation_data=(X_val,y_val),epochs=5,steps_per_epoch=1000)
    
    pickle_out = open("model_trained.p", "wb")
    pickle.dump(model,pickle_out)
    pickle_out.close()

这是我得到的错误:

    pickle.dump(model,pickle_out)
    TypeError: can't pickle _thread.RLock objects

我想保存我训练有素的 model 以便我可以在不同的文件中使用它。 当我尝试使用泡菜保存 model 时,会弹出此错误。 我该怎么做才能消除此错误? 还是有其他方法可以保存我的 model?

Tensorflow 添加了多线程/处理,使其难以腌制。 但是,它们提供了自定义保存方法。

从文档:

def get_model():
    # Create a simple model.
    inputs = keras.Input(shape=(32,))
    outputs = keras.layers.Dense(1)(inputs)
    model = keras.Model(inputs, outputs)
    model.compile(optimizer="adam", loss="mean_squared_error")
    return model


model = get_model()

# Train the model.
test_input = np.random.random((128, 32))
test_target = np.random.random((128, 1))
model.fit(test_input, test_target)

# Calling `save('my_model')` creates a SavedModel folder `my_model`.
model.save("my_model")

# It can be used to reconstruct the model identically.
reconstructed_model = keras.models.load_model("my_model")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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