I am using spyder to implement this example of the car-pole tunnel problem. I got the code from Reinforcement Learning.
I am having problems executing the code. I have tried on both spyder and the terminal. The return error I receive is
Model output "Tensor("activation_1/Identity:0", shape=(?, 16), dtype=float32)" has invalid shape. DQN expects a model that has one dimension for each action, in this case 2.
How can I incorporate the installation of dqn to spyder, so that I am able to execute this code in spyder?
import numpy as np
import gym
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
from keras.optimizers import Adam
from rl.agents.dqn import DQNAgent
from rl.policy import EpsGreedyQPolicy
from rl.memory import SequentialMemory
#Set Relevant variables
ENV_NAME = 'CartPole-v0'
# Get the enviornment and extract the number of actions availible in the Cartpole problem
env = gym.make(ENV_NAME)
np.random.seed(123)
env.seed(123)
nb_actions = env.action_space.n
#hidden layer neural network model.
model = Sequential()
model.add(Flatten(input_shape=(1,) + env.observation_space.shape))
model.add(Dense(16))
model.add(Activation('linear'))
print(model.summary())
policy = EpsGreedyQPolicy()
memory = SequentialMemory(limit=50000, window_length=1)
dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=10, target_model_update=1e-2, policy=policy)
dqn.compile(Adam(lr=1e-3), metrics=['mae'])
#Test model
dqn.test(env, nb_episodes=5, vizualize=True)ter co
Related
I am using tensorflow 2 version for running a sequentional model. I am not sure why it does not support tensorflow2. I also checked the same question, but it doesn't help. can anyone help me to correct the code?
from sklearn.model_selection import train_test_split
import tensorflow
import keras
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from tensorflow.keras import callbacks
from tensorflow.python.keras.layers import Input, Dense, Dropout
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D, AveragePooling2D
from keras.layers.core import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Input
from tensorflow.keras import optimizers
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import plot_model
from tensorflow.keras.models import load_model, Model
from tensorflow.keras.callbacks import EarlyStopping
model = Sequential()
model.add(Conv2D(input_shape=(32,32,4),filters=64,kernel_size=(3,3),padding="same", activation="relu"))
model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Flatten())
model.add(Dense(units=1024,activation="relu"))
model.add(Dense(units=1024,activation="relu"))
model.add(Dense(units=1, activation="linear"))
#compile model
optimizer = tensorflow.keras.optimizers.Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
model.compile(loss='mean_squared_error',
optimizer=optimizer,
metrics=['mae'])
error:
Traceback (most recent call last):
File "/appl/soft/ai/miniconda3/envs/tensorflow-2.0.0/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 66, in _get_default_graph
return tf.get_default_graph()
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
During handling of the above exception, another exception occurred:
RuntimeError: It looks like you are trying to use a version of multi-backend Keras that does not support TensorFlow 2.0. We recommend using `tf.keras`, or alternatively, downgrading to TensorFlow 1.14.
I managed to run the code by editing the section for importing the libraries:
from tensorflow.python.keras.layers import Input, Dense, Flatten, Dropout, Conv2D, MaxPooling2D
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import BatchNormalization
from tensorflow.python.keras import optimizers from tensorflow.python.keras.callbacks import EarlyStopping
import tensorflow as tf
I am just having the following error whilst training a MNIST model(a CNN and FNN) using Keras. Complete beginner at machine learning BTW, so apologies if it is something obvious. Any help appreciated
def base_model():
model = Sequential()
model.add(Dense(pixels, input_dim=pixels, init='normal', activation='relu'))
model.add(Dense(num_op, init='normal', activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
model = base_model()
The above code then leads to TypeError: ('Keyword argument not understood:', 'init')
The imports are as follows...
from keras.datasets import mnist
from tensorflow.keras import Sequential
from keras.layers import Dense, Dropout
from keras.utils import np_utils
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
I am working on VMMR dataset and I am getting this error while starting training callback for the model.
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, TensorBoard, CSVLogger
from tensorflow.keras import optimizers, models
from tensorflow.keras.layers import Dense, Dropout, GlobalAveragePooling2D
from tensorflow.keras import applications
from tensorflow.keras import backend as K
import tensorflow as tf
import os
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.inception_v3 import preprocess_input, decode_predictions
import math
top_layers_file_path="top_layers.iv3.hdf5"
checkpoint = ModelCheckpoint(top_layers_file_path, monitor='loss', verbose=1, save_best_only=True,
mode='min')
tb = TensorBoard(log_dir='./logs', batch_size=val_flow.batch_size, write_graph=True, update_freq='batch')
early = EarlyStopping(monitor="loss", mode="min", patience=5)
csv_logger = CSVLogger('./logs/iv3-log.csv', append=True)
history = model.fit_generator(train_flow, epochs=5, verbose=1,validation_data=val_flow,
validation_steps=math.ceil(val_flow.samples/val_flow.batch_size),
steps_per_epoch=math.ceil(train_flow.samples/train_flow.batch_size),
callbacks=[checkpoint, early, tb, csv_logger]
If your platform is windows,Try this
log_dir='.\logs'
and creating folder manually should always work
hi i'm doing this code in google colab and i have this error No module named 'tensorflow.python.keras.engine.base_layer_v1' in python code with tensor flow keras
i did use tensorflow.keras instead of keras since i use tensorflow v=2.1.0
and keras v=2.3.0-tf
i tried both tensorflow v=2.1.0 and v=2.2.0-rc2
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.layers import Dense, Embedding, LSTM, SpatialDropout1D
from sklearn.model_selection import train_test_split
MAX_NB_WORDS=50000
EMBEDDING_DIM=100
model = tf.keras.Sequential()
model.add(Embedding(MAX_NB_WORDS, EMBEDDING_DIM, input_length=train.shape[1]))
model.add(SpatialDropout1D(0.2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(13, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
epochs = 5
batch_size = 64
history = model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[EarlyStopping(monitor='val_loss', patience=3, min_delta=0.0001)])
accr = model.evaluate(x_test,y_test)
print('Test set\n Loss: {:0.3f}\n Accuracy: {:0.3f}'.format(accr[0],accr[1]))
I had similar error while working with gaborNet-CNN. I tired following and it worked in my case.
import numpy as np
from matplotlib import pyplot as plt
from tqdm import tqdm
import keras
from keras import backend as K
from keras import activations, initializers, regularizers, constraints, metrics
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model
from keras.layers import (Dense, Dropout, Activation, Flatten, Reshape, Layer,
BatchNormalization, LocallyConnected2D,
ZeroPadding2D, Conv2D, MaxPooling2D, Conv2DTranspose,
GaussianNoise, UpSampling2D, Input)
from keras.utils import conv_utils, multi_gpu_model
from keras.layers import Lambda
from keras.engine import Layer, InputSpec
from keras.legacy import interfaces
in my case, I just reinstall keras and it works
I am following an online course through linkedin regrading the Building of models through Keras.
This is my code. (This is claimed to work)
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import *
training_data_df = pd.read_csv("sales_data_training_scaled.csv")
X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values
# Define the model
model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu', name='layer_1'))
model.add(Dense(100, activation='relu', name='layer_2'))
model.add(Dense(50, activation='relu', name='layer_3'))
model.add(Dense(1, activation='linear', name='output_layer'))
model.compile(loss='mean_squared_error', optimizer='adam')
# Create a TensorBoard logger
logger = keras.callbacks.TensorBoard(
log_dir='logs',
write_graph=True,
histogram_freq=5
)
# Train the model
model.fit(
X,
Y,
epochs=50,
shuffle=True,
verbose=2,
callbacks=[logger]
)
# Load the separate test data set
test_data_df = pd.read_csv("sales_data_test_scaled.csv")
X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values
test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print("The mean squared error (MSE) for the test data set is: {}".format(test_error_rate))
I get the following error when the following code was executed.
Using TensorFlow backend.
2020-01-16 13:58:14.024374: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2020-01-16 13:58:14.037202: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fc47b436390 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-01-16 13:58:14.037211: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version
Traceback (most recent call last):
File "/Users/himsaragallage/Documents/Building_Deep_Learning_apps/06/model_logging final.py", line 35, in <module>
callbacks=[logger]
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 1239, in fit
validation_freq=validation_freq)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training_arrays.py", line 119, in fit_loop
callbacks.set_model(callback_model)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/callbacks/callbacks.py", line 68, in set_model
callback.set_model(model)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/callbacks/tensorboard_v2.py", line 116, in set_model
super(TensorBoard, self).set_model(model)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/tensorflow_core/python/keras/callbacks.py", line 1532, in set_model
self.log_dir, self.model._get_distribution_strategy()) # pylint: disable=protected-access
AttributeError: 'Sequential' object has no attribute '_get_distribution_strategy'
Process finished with exit code 1
While I was trying to Debug
I found out that this error was caused because I am trying to use a tensorboard logger. More accurately. When I add callbacks=[logger]. Without that line of code the program runs without any errors. But Tensorboard won't be used.
Please suggest me a method in which I can eliminate the error successfully run the above mentioned python script.
Hope you are referring to this LinkedIn Keras Course.
Even I faced the Same Error when I have used Tensorflow Version 2.1. However, after downgrading the Tensorflow Version and with slight modifications in the code, I could invoke Tensorboard.
Working Code is shown below:
import pandas as pd
import keras
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
training_data_df = pd.read_csv("sales_data_training_scaled.csv")
X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values
# Define the model
model = Sequential()
model.add(Dense(50, input_dim=9, activation='relu', name='layer_1'))
model.add(Dense(100, activation='relu', name='layer_2'))
model.add(Dense(50, activation='relu', name='layer_3'))
model.add(Dense(1, activation='linear', name='output_layer'))
model.compile(loss='mean_squared_error', optimizer='adam')
# Create a TensorBoard logger
logger = tf.keras.callbacks.TensorBoard(
log_dir='logs',
write_graph=True,
histogram_freq=5
)
# Train the model
model.fit(
X,
Y,
epochs=50,
shuffle=True,
verbose=2,
callbacks=[logger]
)
# Load the separate test data set
test_data_df = pd.read_csv("sales_data_test_scaled.csv")
X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values
test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print("The mean squared error (MSE) for the test data set is: {}".format(test_error_rate))
You may find this post useful.
So instead of importing from keras (i.e.)
from keras.models import Sequential
import from tensorflow:
from tensorflow.keras.models import Sequential
And this of course applies to most other imports as well.
This is just a lucky guess because I can't run your code, but hope it helps!
I would recommend not mixing keras and tf.keras. Those are different projects as keras is the original, multi-backend project and tf.keras is the version integrated into tensorflow. Keras will stop supporting other backends but tensorflow so it's adviced to switch to it. Check https://keras.io/#multi-backend-keras-and-tfkeras
An easy way to do that is importing keras from tensorflow:
import tensorflow as tf
import tensorflow.keras as keras
#import keras
import keras.backend as K
from keras.models import Model, Sequential, load_model
from keras.layers import Dense, Embedding, Dropout, Input, Concatenate
print("Python: "+str(sys.version))
print("Tensorflow version: "+tf.__version__)
print("Keras version: "+keras.__version__)
Python: 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0]
Tensorflow version: 2.1.0
Keras version: 2.2.4-tf
It seems that your python environment is mixing imports from keras and tensorflow.keras. Try to use Sequential module like this:
model = tensorflow.keras.Sequential()
I had same error, the problem was tensorflows version.
!pip install tensorflow==2.3.0
fixed that