簡體   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