AttributeError: module 'keras.layers' has no attribute 'regularizers' - python

I try to run a Python project from Git for the first time. After having installed all needed librariers I started the program and I received following error:
AttributeError: module 'keras.layers' has no attribute 'regularizers'
Source:
from keras import layers, models, optimizers
from keras import backend as K
class Actor:
'''
Actor(policy) Model
'''
def __init__(self, state_size, action_size):
self.state_size = state_size
self.action_size = action_size
self.build_model()
def build_model(self):
states = layers.Input(shape=(self.state_size, ), name='states')
net = layers.Dense(units=16, kernel_regularizer=layers.regularizers.l2(1e-6))(states)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
net = layers.Dense(units=32, kernel_regularizer=layers.regularizers.l2(1e-6))(net)
net = layers.BatchNormalization()(net)
net = layers.Activation('relu')(net)
actions = layers.Dense(units=self.action_size, activation='softmax', name='actions')(net)
self.model = models.Model(inputs=states, outputs=actions)
action_gradients = layers.Input(shape=(self.action_size, ))
loss = K.mean(-action_gradients * actions)
optimizer = optimizers.Adam(lr=.0001)
updates_op = optimizer.get_updates(params=self.model.trainable_weights, loss=loss)
self.train_fn = K.function(
inputs = [self.model.input, action_gradients, K.learning_phase()],
outputs = [],
updates = updates_op
)

I think the program was written in TensorFlow version 1 and also keras version 1. You may be solved the problem by writing that way
net = layers.Dense(32, kernel_regularizer=tensorflow.keras.regularizers.L1(1e-6))(states)
but will occur so many errors. As there used some functionality that has been removed in later versions such as
optimizer.get_updates
no longer works. So, better work with later version

Related

OpenAI-Gym and Keras-RL: DQN expects a model that has one dimension for each action

I am trying to set a Deep-Q-Learning agent with a custom environment in OpenAI Gym. I have 4 continuous state variables with individual limits and 3 integer action variables with individual limits.
Here is the code:
#%% import
from gym import Env
from gym.spaces import Discrete, Box, Tuple
import numpy as np
#%%
class Custom_Env(Env):
def __init__(self):
# Define the state space
#State variables
self.state_1 = 0
self.state_2 = 0
self.state_3 = 0
self.state_4_currentTimeSlots = 0
#Define the gym components
self.action_space = Box(low=np.array([0, 0, 0]), high=np.array([10, 20, 27]), dtype=np.int)
self.observation_space = Box(low=np.array([20, -20, 0, 0]), high=np.array([22, 250, 100, 287]),dtype=np.float16)
def step(self, action ):
# Update state variables
self.state_1 = self.state_1 + action [0]
self.state_2 = self.state_2 + action [1]
self.state_3 = self.state_3 + action [2]
#Calculate reward
reward = self.state_1 + self.state_2 + self.state_3
#Set placeholder for info
info = {}
#Check if it's the end of the day
if self.state_4_currentTimeSlots >= 287:
done = True
if self.state_4_currentTimeSlots < 287:
done = False
#Move to the next timeslot
self.state_4_currentTimeSlots +=1
state = np.array([self.state_1,self.state_2, self.state_3, self.state_4_currentTimeSlots ])
#Return step information
return state, reward, done, info
def render (self):
pass
def reset (self):
self.state_1 = 0
self.state_2 = 0
self.state_3 = 0
self.state_4_currentTimeSlots = 0
state = np.array([self.state_1,self.state_2, self.state_3, self.state_4_currentTimeSlots ])
return state
#%% Set up the environment
env = Custom_Env()
#%% Create a deep learning model with keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.optimizers import Adam
def build_model(states, actions):
model = Sequential()
model.add(Dense(24, activation='relu', input_shape=states))
model.add(Dense(24, activation='relu'))
model.add(Dense(actions[0] , activation='linear'))
return model
states = env.observation_space.shape
actions = env.action_space.shape
print("env.observation_space: ", env.observation_space)
print("env.observation_space.shape : ", env.observation_space.shape )
print("action_space: ", env.action_space)
print("action_space.shape : ", env.action_space.shape )
model = build_model(states, actions)
print(model.summary())
#%% Build Agent wit Keras-RL
from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory
def build_agent (model, actions):
policy = BoltzmannQPolicy()
memory = SequentialMemory(limit = 50000, window_length=1)
dqn = DQNAgent (model = model, memory = memory, policy=policy,
nb_actions=actions, nb_steps_warmup=10, target_model_update= 1e-2)
return dqn
dqn = build_agent(model, actions)
dqn.compile(Adam(lr=1e-3), metrics = ['mae'])
dqn.fit (env, nb_steps = 4000, visualize=False, verbose = 1)
When I run this code I get the following error message
ValueError: Model output "Tensor("dense_23/BiasAdd:0", shape=(None, 3), dtype=float32)" has invalid shape. DQN expects a model that has one dimension for each action, in this case (3,).
thrown by the line dqn = DQNAgent (model = model, memory = memory, policy=policy, nb_actions=actions, nb_steps_warmup=10, target_model_update= 1e-2)
Can anyone tell me, why this problem is occuring and how to solve this issue? I assume it has something to do with the built model and thus with the action and state spaces. But I could not figure out what exactly the problem is.
Reminder on the bounty: My bounty is expiring quite soon and unfortunately, I still have not received any answer. If you at least have a guess how to tackle that problem, I'll highly appreciate if you share your thoughts with me and I would be quite thankful for it.
As we talked about in the comments, it seems that the Keras-rl library is no longer supported (the last update in the repository was in 2019), so it's possible that everything is inside Keras now. I take a look at Keras documentation and there are no high-level functions to build a reinforcement learning model, but is possible to use lower-level functions to this.
Here is an example of how to use Deep Q-Learning with Keras: link
Another solution may be to downgrade to Tensorflow 1.0 as it seems the compatibility problem occurs due to some changes in version 2.0. I didn't test, but maybe the Keras-rl + Tensorflow 1.0 may work.
There is also a branch of Keras-rl to support Tensorflow 2.0, the repository is archived, but there is a chance that it will work for you
Adding a flatten layer before the final output can solve this error. Example:
def build_model(states, actions):
model = Sequential()
model.add(Dense(24, activation='relu', input_shape=states))
model.add(Dense(24, activation='relu'))
model.add(Flatten())
model.add(Dense(actions[0] , activation='linear'))
return model

How to call model.evaluate() in keras callback?

Short story: I am building an Autoencoder and would like to store reconstructed images along the way of training. I made a custom callback that writes images to the summary. The only thing that remains is to call my reconstruction layer inside of callback.on_epoch_end(...). How can I get access to a named layer inside of the callback and run a calculation?
Layer definition:
decode = layers.Conv2D(1, (5, 5), name='wwae_decode', activation='sigmoid', padding='same')(conv3)
Callback definition:
class TensorBoardImage(tf.keras.callbacks.Callback):
def __init__(self, tag, logdir):
super().__init__()
self.tag = tag
self.logdir = logdir
def on_epoch_end(self, epoch, logs={}):
img_stack = self.validation_data[0][:3]
# TODO: run img_stack through 'wwae_decode' layer first
# img_stack = self?model?get_layer('wwae_decode').evaluate(img_stack) # ????
single_image = merge_axis(img_stack, target_axis=2)
summary_str = []
single_image = (255 * single_image).astype('uint8')
summary_str.append(tf.Summary.Value(tag=self.tag, image=make_image(single_image)))
# multiple summaries can be appended
writer = tf.summary.FileWriter(self.logdir)
writer.add_summary(tf.Summary(value=summary_str), epoch)
return
If this is the last layer in your model (i.e. output layer), then you can simply call predict method of the model instance inside the callback:
# ...
img_stack = self.validation_data[0][:3]
preds_img_stack = self.model.predict(img_stack)
# ...
Alternatively, you can directly compute a layer's output by defining a backend function:
from keras import backend as K
func = K.function(model.inputs + [K.learning_phase()], [model.get_layer('wwae_decode').output])
# ...
img_stack = self.validation_data[0][:3]
preds_img_stack = func([img_stack, 0])[0]
# ...
For more information, I suggest you to read the relevant section in Keras FAQ: How can I obtain the output of an intermediate layer?

Keras doesn't seem to correctly load a trained model

Following along with a tutorial to learn keras i've hit a bit of a snag.
i have some code to solve the lunar lander problem, which seems to train the agent up and get pretty good scores after many iterations of training (eg for the Lunar lander problem hes getting scores between 200 - 400 usually), but when i load in the trained model its like he starts all over. Not sure if I'm doing something wrong or if keras is , really need some advice here.
At the bottom i've included some of the final scores its getting , then some of the scores its getting when i rerun
import random
import os
import gym
import numpy as np
from collections import deque
from keras.models import Sequential
from keras.models import load_model
from keras.layers import Dense
from keras.optimizers import Adam
EPISODES = 1000
class DQNAgent:
def __init__(self, state_size, action_size):
self.state_size = state_size
self.action_size = action_size
self.memory = deque(maxlen=2000)
self.gamma = 0.95 # discount rate
self.epsilon = 1.0 # exploration rate
self.epsilon_min = 0.01
self.epsilon_decay = 0.995
self.learning_rate = 0.001
self.model = self._build_model()
def _build_model(self):
# Neural Net for Deep-Q learning Model
model = Sequential()
model.add(Dense(24, input_dim=self.state_size, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(self.action_size, activation='linear'))
model.compile(loss='mse',
optimizer=Adam(lr=self.learning_rate))
return model
def remember(self, state, action, reward, next_state, done):
self.memory.append((state, action, reward, next_state, done))
def act(self, state):
if np.random.rand() <= self.epsilon:
return random.randrange(self.action_size)
act_values = self.model.predict(state)
return np.argmax(act_values[0]) # returns action
def replay(self, batch_size):
minibatch = random.sample(self.memory, batch_size)
for state, action, reward, next_state, done in minibatch:
target = reward
if not done:
target = (reward + self.gamma *
np.amax(self.model.predict(next_state)[0]))
target_f = self.model.predict(state)
target_f[0][action] = target
self.model.fit(state, target_f, epochs=1, verbose=0)
if self.epsilon > self.epsilon_min:
self.epsilon *= self.epsilon_decay
def load(self, name):
#self.model.load(name)
pass
def save(self, name):
self.model.save(name)
#if __name__ == "__main__":
loaded = False
env = gym.make('LunarLander-v2')
state_size = env.observation_space.shape[0]
action_size = env.action_space.n
agent = DQNAgent(state_size, action_size)
# agent.load("./save/cartpole-dqn.h5")
if os.path.exists('cart_save.h5') == True:
#agent.model.load('cart_save.h5')
agent.model = load_model('cart_save.h5')
loaded = False
done = False
batch_size = 32
print
print loaded
print
for e in range(EPISODES):
state = env.reset()
state = np.reshape(state, [1, state_size])
time_rec = -1
total_reward = 0
end = False
#for time in range(500):
while not end:
time_rec += 1
# env.render()
action = agent.act(state)
next_state, reward, done, _ = env.step(action)
#reward = reward if not done else -10
total_reward += reward
next_state = np.reshape(next_state, [1, state_size])
agent.remember(state, action, total_reward, next_state, done)
state = next_state
if loaded:
env.render()
if done:
print("episode: {}/{}, score: {}, e: {:.2}"
.format(e, EPISODES, total_reward, agent.epsilon))
break
if len(agent.memory) > batch_size:
agent.replay(batch_size)
if done and e > 50 and agent.epsilon < .05 and total_reward > 200:
agent.model.save("cart2_save.h5")
print
print "saved file"
print
if e == 200:
agent.model.save("cart_final.h5")
if e == 400:
agent.model.save("cart_final.h5")
if e == 600:
agent.model.save("cart_final.h5")
if e >= 1000:
agent.model.save("cart_final.h5")
You might wanna try:
from tensorflow.keras.models import load_model
#Save the whole model
model.save('my_model.h5')
new_model = load_model('my_model.h5')
Reference: https://jovianlin.io/saving-loading-keras-models/
The question is quite old but maybe it can help someone else. I was having the same issue and actually realized it has nothing to do with loading the model.
The issue is this line:
def __init__(self, state_size, action_size):
...
self.epsilon = 1.0 # exploration rate
When you train, your epsilon is decreased at each episode so you have less exploration.
But if you instanciate a new agent and load the model, you have the correct model but a full exploration agent (epsilon=1).
When loading from a trained model, you need epsilon=0 as you are not doing any training anymore (or have a different inference function for prediction outside of training ignoring epsilon).
Another way to look at it is to realise that for predictions only (no training), you don't need an agent. You can use the model alone.
model = load_model("model_name.h5")
action = model.predict(state)
I bet you aren't loading what you think you're loading. Maybe I missed it, but the code you posted doesn't seem to include writing your trained model to disk so I'm not sure how the model you are loading got there. Additionally, you check for the existence of cart_save.h5, but then ignore that model and (try to) load 900.h5. Per your outputs, it seems 900.h5 probably isn't the model you want to be trying to load.
It's a little hard to read your code because the indentation seems to have been messed up a bit. Also, are you using python 2? You should really, REALLY switch to python 3.
My recommendation is to move all of your saved models into a subdirectory your script doesn't know about like "./archive" or something like that and try re-running it fresh. That way you can be sure that the model you are loading is one that was a saved checkpoint produced by the most recent execution of the script, because it isn't clear to me that this is currently the case.

Watson generated Pytorch results in: "ValueError: optimizer got an empty parameter list"

I am experimenting Watson Neural Network Modeler.
I've create a model from the built in demo "Single Convolution layer on MNIST". The only customization I did was to specify the training data files.
I then exported the Pytorch code and I am trying to run in on my local computer.
The generated code is pretty readable. The relevant code excerpt is:
# Define network architecture
class Net(nn.Module):
def __init__(self, inp_c):
super(Net, self).__init__()
def forward(self, ImageData_4, target):
Convolution2D_9 = self.Convolution2D_9(ImageData_4)
ReLU_1 = self.ReLU_1(Convolution2D_9)
Pooling2D_8 = self.Pooling2D_8(ReLU_1)
Flatten_2 = Pooling2D_8.view(-1, 10816)
Dense_3 = self.Dense_3(Flatten_2)
Softmax_5 = self.Softmax_5(Dense_3)
Accuracy_6 = torch.topk(Softmax_5, 1)[0]
CrossEntropyLoss_7 = self.CrossEntropyLoss_7(Softmax_5, target)
return Softmax_5, Accuracy_6
# Model Initialization
inp_c = 1
model = Net(inp_c)
model.cuda()
# Define optimizer
learning_rate = 0.001000
decay = 0.000000
beta_1 = 0.900000
beta_2 = 0.999000
optim = optim.Adam(
model.parameters(),
lr=learning_rate,
betas=(beta_1, beta_2),
weight_decay=decay)
I am getting the error:
"ValueError: optimizer got an empty parameter list"
on the optim = optim.Adam() statement.
Is there any Watson user/expert over there to bring some light on this issue? I am basically running the demo. It was not supposed to fail.
Thanks!

How to utilize all GPUs when dealing with pytorch code?

I have 2 GPUs and when I am working with pytorch code, only one GPU is used. I tried CUDA_VISIBLE_DEVICES=0,1 python xxx.py, but occurs
'CUDA_VISIBLE_DEVICES: command not found'
problems. I have also tried to add the following lines in object py file:
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
but still only one GPU is utilized.
You need to parallelize the training data to each GPU seperatly. Data Parallelism is implemented using torch.nn.DataParallel. An example from the pytorch documentation:
import torch
import torch.nn as nn
class DataParallelModel(nn.Module):
def __init__(self):
super().__init__()
self.block1 = nn.Linear(10, 20)
# wrap block2 in DataParallel
self.block2 = nn.Linear(20, 20)
self.block2 = nn.DataParallel(self.block2)
self.block3 = nn.Linear(20, 20)
def forward(self, x):
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
return x

Categories