Keras symbolic inputs/outputs do not implement __len__ Error - python

I want to build an AI to solve an optimization problem in a given environment, but I get the following error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-352-765c5782fe72> in <module>()
1 model=Model(inputs=input_layer,outputs=output)
----> 2 model.compile(optimizer='adam',loss=-RewardFn,metrics=['acc'])
3 model.summary()
1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/keras_tensor.py in __len__(self)
219
220 def __len__(self):
--> 221 raise TypeError('Keras symbolic inputs/outputs do not '
222 'implement `__len__`. You may be '
223 'trying to pass Keras symbolic inputs/outputs '
TypeError: Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.
I found out about this error and it is said to be a problem with tensorflow. But I don't know how to solve it. This is my model
!pip install keras-rl2
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from google.colab import files
import io
# %matplotlib inline
import seaborn as sns
sns.set(style='darkgrid')
uploaded=files.upload()
cols=['node1x','node2x','node3x','node4x','node1y','node2y','node3y','node4y','Rmin']
Dataset=pd.read_csv(io.StringIO(uploaded['DNNsamples.csv'].decode('utf-8')),names=cols,header=None)
Dataset.head(20)
from sklearn.model_selection import train_test_split
X_train,X_test=train_test_split(Dataset,test_size=0.2,random_state=42)
from tensorflow.keras.layers import Input,Dense,Activation,Dropout,Flatten
from tensorflow.keras.models import Model
------
input_layer=Input(shape=(Dataset.shape[1],))
dense_layer1=Dense(21,activation='relu')(input_layer)
dense_layer2=Dense(21,activation='relu')(dense_layer1)
dense_layer3=Dense(21,activation='relu')(dense_layer2)
dense_layer4=Dense(21,activation='relu')(dense_layer3)
dense_layer5=Dense(21,activation='relu')(dense_layer4)
dense_layer6=Dense(21,activation='relu')(dense_layer5)
output=Dense(outputss,activation='sigmoid')(dense_layer6)
-----
RewardFn=Ravg+Constraint1+Constraint2+Constraint3+Constraint4+Constraint5
tf.shape(RewardFn)
model=Model(inputs=input_layer,outputs=output)
model.compile(loss=-RewardFn,optimizer='adam',metrics=['acc'])
model.summary()
Could it be a problem to use input and output values ​​in a loss function?
I use Google Colab.

You do not need to specifically install the Keras package separately. You can import Keras from TensorFlow. Also, please provide the right alias while importing Input as below. Input is submodule of tf.keras API, not part of tensorflow.keras.layers API.
from tensorflow import keras
from tensorflow.keras import Input
from tensorflow.keras.layers import Dense,Activation,Dropout,Flatten
Please check the tensorflow and keras version should be as per this tested build configurations. Let us know if the issue still persists.

Related

cannot import name 'TimeSeriesGenerator' from 'keras.preprocessing.sequence'

I'm new to keras and trying to work with this, however, I have problem in the imports.
I can import all the following packages:
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Nadam
from tensorflow.keras.layers import Input, LSTM, Dense
from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau, TerminateOnNaN
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator
but when I try to import the time series generatore I get an error:
from keras.preprocessing.sequence import TimeSeriesGenerator
>>>mportError: cannot import name 'TimeSeriesGenerator' from 'keras.preprocessing.sequence' (C:\path\myuser\anaconda3\envs\keras1\lib\site-packages\keras\preprocessing\sequence.py)
This happens after I have created new environment, installed first tensorflow, but nothing changes and I keep getting this error.
What am I missing? how can I solve it and use the timeseries generator?
You misspelled the import, it should be TimeseriesGenerator (lowercase s)

Issue with opening h5 file with Python code in MATLAB environment

I have an issue with calling Python code in MATLAB. My Python code involves predicting the battery state of charge using LSTM with attention ANN based on the inputs sent from MATLAB. The prediction is then sent back to MATLAB. I already have previously trained weights and biases saved in an h5 file, which is loaded and used in the Python code. Below is the Python code:
import pandas as pd
import numpy as np
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from tensorflow import keras
from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras import optimizers
import matplotlib.pyplot as plt
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
from tensorflow.keras import backend as K
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import load_model
from tensorflow.keras.layers import Dropout, InputLayer
import h5py
#to create sequential data
def create_inout_sequences(input_data, tw):
inout_seq = []
L = len(input_data)
for i in range(L-tw):
train_seq = input_data[i:i+tw]
#train_out = output_data[i:i+tw]
inout_seq.append(train_seq)
return inout_seq
def search(inputs):
class attention(Layer):
def __init__(self, return_sequences=True,**kwargs):
self.return_sequences = return_sequences
super(attention,self).__init__()
def build(self, input_shape):
self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
initializer="normal")
self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
initializer="zeros")
super(attention,self).build(input_shape)
def call(self, x):
e=(K.dot(x,self.W)+self.b)
a = K.softmax(e, axis=1)
output = x*a
if self.return_sequences:
return output
return K.sum(output, axis=1)
def get_config(self):
# For serialization with 'custom_objects'
config = super().get_config()
config['return_sequences'] = self.return_sequences
return config
#convert test data to sequential form
inputs=np.array(inputs)
inputs=np.tile(inputs, (36, 1))
inputs_new=create_inout_sequences(inputs, 35)
inputs_new=np.array(inputs_new)
model1 = Sequential()
model1.add(InputLayer(input_shape=(35,5)))
model1.add((LSTM(22, return_sequences=True)))
model1.add(attention(return_sequences=False))
model1.add(Dense(104, activation="relu"))
model1.add(Dropout(0.2))
model1.add(Dense(1, activation="sigmoid"))
lr_schedule = keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01,
decay_steps=10000,
decay_rate=0.99)
model1.compile(optimizer=tf.keras.optimizers.Adam(epsilon=1e-08,learning_rate=lr_schedule),loss='mse')
#call previously trained weights
model1.load_weights('SOC_weights.h5')
x=float(model1.predict(inputs_new, batch_size=100,verbose=0))
return x # send prediction to Matlab
Note: I am using Python 3.6, tensorflow version: 2.5, keras version: 2.4.3, h5py version: 3.1.0, cython version: 0.28
I am able to run this code without any error on Python, but have issues when used in MATLAB 2020a... below is my MATLAB code:
pyenv('Version','3.6');
py.importlib.import_module('tensorflow');
py.importlib.import_module('testingSOC'); % file containing the Python codes
inputs=[0.555555556,0.435139205,0.68313128,0.499987472,0.241225578];% test inputs
SOC_output=py.testingSOC.search(inputs)
Below is the error received on Matlab:
Error using training>load_weights (line 2312)
Python Error: ImportError: `load_weights` requires h5py when loading weights from HDF5.
Error in testingSOC>search (line 87)
the error looks like h5py is not identified by MATLAB, so I have tried reinstalling h5py by using the command prompt (I am using Windows 10):
pip uninstall h5py
pip install h5py
but no changes...
I have also tried with tensorflow version: 2.2, keras version 2.4.3, h5py version 2.10 and cython version 0.29 but still get the same error.
I would really appreciate if you guys can provide an insight in solving this issue, and if there are any fundamental parts that I have missed. I would be glad to share more details if required.
Thanks!
Thanks to #TimRoberts for pointing out about including 'py.importlib.import_module('h5py')' which helped me in resolving this issue.Below is my solution, for those who would like to refer:
When I included 'py.importlib.import_module('h5py')' in my matlab codes, I received the following error:
Error using h5>init h5py.h5 (line 1)
Python Error: ImportError: DLL load failed: The specified procedure could not be found.
It looks like Python environment seems to use Matlab's h5 library in my case, which does not have the same features as Python's h5 library...I found that there is an option of running Python codes as a separate process which seems to be working for me (as seen in this link):
https://www.mathworks.com/help/matlab/matlab_external/out-of-process-execution-of-python-functionality.html?searchHighlight=out%20of%20process%20python&s_tid=srchtitle

'Model' object has no attribute 'loss_functions'

I have completed a udacity nanodegree for NLP. I used the udacity platform for the project, but I am now trying to use my own local machine to train models etc.
I've finally gotten my GPU/tensorflow issues worked out(I think), but I'm running into some problems that I believe are related to the versions of tensorflow that udacity was using.
I am currently using TensorFlow 2.2
Specifically, I am getting an error from a validation step the project uses to list the loss function.
def _test_model(model, input_shape, output_sequence_length, french_vocab_size):
if isinstance(model, Sequential):
model = model.model
print(model.loss_functions)
When this is called I get the "'Model' object has no attribute 'loss_functions'" error.
The model is built with the below code.
def simple_model(input_shape, output_sequence_length, english_vocab_size, french_vocab_size):
"""
Build and train a basic RNN on x and y
:param input_shape: Tuple of input shape
:param output_sequence_length: Length of output sequence
:param english_vocab_size: Number of unique English words in the dataset
:param french_vocab_size: Number of unique French words in the dataset
:return: Keras model built, but not trained
"""
# TODO: Build the layers
learning_rate = 0.01
#Config Model
inputs = Input(shape=input_shape[1:])
hidden_layer = GRU(output_sequence_length, return_sequences=True)(inputs)
outputs = TimeDistributed(Dense(french_vocab_size, activation='softmax'))(hidden_layer)
#Create Model from parameters defined above
model = keras.Model(inputs=inputs, outputs=outputs)
#loss_function = 'sparse_categorical_crossentropy'
loss_fn = keras.losses.SparseCategoricalCrossentropy()
model.compile(loss=loss_fn,optimizer=Adam(learning_rate),metrics=['accuracy'])
I am using the below libraries along the way
import tensorflow as tf
from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow import keras
import collections
import helper
import numpy as np
import project_tests as tests
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import GRU, Input, Dense, TimeDistributed, Activation, RepeatVector, Bidirectional, Dropout
from tensorflow.keras.layers.embeddings import Embedding
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import sparse_categorical_crossentropy
I can just comment out this check for the loss functions, but I would really like to understand what happened.
Thanks
I think the API changed in Tensorflow 2, does the following work:
model.compiled_loss._get_loss_object(model.compiled_loss._losses).fn

The added layer must be an instance of class Layer. Found: keras.layers.convolutional.Conv2DTranspose

Can you help me with this error?
TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.convolutional.Conv2DTranspose object at 0x7f5dc629f240>
I get this when I try to execute the following line
decoder.add(Deconvolution2D(64, 3, 3, subsample=(1, 1), border_mode='same'))
My imports are:
from keras.layers import Layer
from keras.layers import Input
from keras.layers.convolutional import Deconvolution2D
According to Installing Keras: To use Keras, we will need to have the TensorFlow package installed. Once TensorFlow is installed.
Now import Keras as shown below
from tensorflow import keras
Now Deconvolution2Dlayer has been renamed Conv2DTranspose layer.
Now you can import layers as shown below
from tensorflow.keras.layers import Input, Conv2DTranspose
For more information you can refer here

I can't load my trained h5 model with load.models(), how do I fix this error?

So I think tensorflow.keras and the independant keras packages are in conflict and I can't load my model, which I have made with transfer learning.
Import in the CNN ipynb:
!pip install tensorflow-gpu==2.0.0b1
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
Loading this pretrained model
base_model = keras.applications.xception.Xception(weights="imagenet",
include_top=False)
avg = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation="softmax")(avg)
model = keras.models.Model(inputs=base_model.input, outputs=output)
Saving with:
model.save('Leavesnet Model 2.h5')
Then in the new ipynb for the already trained model (the imports are the same as in the CNN ipynb:
from keras.models import load_model
model =load_model('Leavesnet Model.h5')
I get the error:
AttributeError Traceback (most recent call last)
<ipython-input-4-77ca5a1f5f24> in <module>()
2 from keras.models import load_model
3
----> 4 model =load_model('Leavesnet Model.h5')
13 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in placeholder(shape, ndim, dtype, sparse, name)
539 x = tf.sparse_placeholder(dtype, shape=shape, name=name)
540 else:
--> 541 x = tf.placeholder(dtype, shape=shape, name=name)
542 x._keras_shape = shape
543 x._uses_learning_phase = False
AttributeError: module 'tensorflow' has no attribute 'placeholder'
I think there might be a conflict between tf.keras and the independant keras, can someone help me out?
Yes, there is a conflict between tf.keras and keras packages, you trained the model using tf.keras but then you are loading it with the keras package. That is not supported, you should use only one version of this package.
The specific problem is that you are using TensorFlow 2.0, but the standalone keras package does not support TensorFlow 2.0 yet.
Try to replace
from keras.models import load_model
model =load_model('Leavesnet Model.h5')
with
model = tf.keras.models.load_model(model_path)
It works for me, and I am using:
tensorflow version: 2.0.0
keras version: 2.3.1
You can check the following:
https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model?version=stable
I think downgrading your keras or tensorflow will not be that much suitable because you need to retrain your model for various dependencies. Why not try to load the weights instead of loading the model?
here is a piece of code
import tensorflow as tf
{your code here}
#save the weights
model.save_weights('model_checkpoint')
#initialise the model again (example - MobileNetv2)
encoder = MobileNetV2(input_tensor=inputs, weights="imagenet", include_top=False, alpha=0.35)
#load the weights
encoder.load_weights('model_checkpoint')
and you are good to go

Categories

Resources