i am running keras for a while now. saving models working fine using:
model.save('meta/model.h5')
as FAQ is suggesting ( https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model )
but loding a model like:
model = load_model('meta/model.h5')
fails with:
TypeError: load_model() takes 0 positional arguments but 1 was given
How will the machine know where to get model with 0 arguments ^^.
I am running:
pip show keras
Name: Keras
Version: 2.0.2
Summary: Deep Learning for Python
Home-page: https://github.com/fchollet/keras
Author: Francois Chollet
Author-email: [email protected]
License: MIT
Location: /usr/lib/python3.6/site-packages
Requires: pyyaml, six, theano
How can i help to fix this?
Don't know much about python 3, but from the readme:
"Keras is compatible with: Python 2.7-3.5." and it looks like you're using 3.6.
Trying 3.5 might be a good place to start
The code for models.load_model is pretty straightforward. It seems like there's something else going on. Can you post a small example program that shows the failure?
@JimBiardCics
Yes of course. Here is what i do:
`#!/usr/bin/python3
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "" # just made empty for this test script
from keras import optimizers
import keras.backend as K
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.callbacks import Callback
from keras.utils import plot_model
from keras.models import load_model
size_inp = 200 #len(source_train[0]) # input
size_la1 = size_inp * 4 # n 1
size_la2 = size_inp * 8 # n 2
size_la3 = size_inp * 4 # n 3
size_out = 1 #len(target_train[0]) # output
model = Sequential()
model.add( Dense(input_dim=size_inp, units=size_la1) )
model.add( Activation("relu") )
model.add( Dense(units=size_la2) )
model.add( Activation("relu") )
model.add( Dense(units=size_la3) )
model.add( Activation("relu") )
model.add( Dense(units=size_out) )
model.compile(loss='mean_absolute_error', optimizer='rmsprop', metrics=['accuracy'])
model.save('model.h5')
del model
model = load_model('model.h5')`
This works for me sucessfully without fitting the model.
lets see whats happening if we fit the weights.
`#!/usr/bin/python3
import os
import numpy as np
import math
os.environ["CUDA_VISIBLE_DEVICES"] = "" # just made empty for this test script
from keras import optimizers
import keras.backend as K
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.callbacks import Callback
from keras.utils import plot_model
from keras.models import load_model
np.random.seed(0)
x = np.linspace(0, 2 * math.pi, 200)
sine = np.sin(x)
err = np.random.normal(0, 0.2, len(sine))
y = sine + err
source_train = np.array(x, ndmin=2).T
target_train = np.array(y, ndmin=2).T
size_inp = len(source_train) # input
size_la1 = size_inp * 4 # n 1
size_la2 = size_inp * 8 # n 2
size_la3 = size_inp * 2 # n 3
size_out = len(target_train) # output
model = Sequential()
model.add( Dense(input_dim=1, units=size_la1) )
model.add( Activation("relu") )
model.add( Dense(units=size_la2) )
model.add( Activation("relu") )
model.add( Dense(units=size_la3) )
model.add( Activation("relu") )
model.add( Dense(units=1) )
model.compile(loss='mean_absolute_error', optimizer='rmsprop', metrics=['accuracy'])
model.fit( source_train,
target_train,
epochs=100,
verbose=1,
batch_size=64 )
model.save('model.h5')
del model
model = load_model('model.h5')`
i still have no idea what wented wrong earlier but this one works, too 馃憤
i am really sorry for bothering arround here! -> CLOSED
Alternately, you could try this:
global model
model = tf.keras.models.load_model('model.h5')
It worked for me
Most helpful comment
Alternately, you could try this:
global modelmodel = tf.keras.models.load_model('model.h5')It worked for me