Save the Keras model error: AttributeError: 'numpy.dtype' object has no attribute 'item' - tensorflow

I have tried to save my Keras model in pycharm where I got the error, this is how I created the model:
main_input = Input(shape=(X_train.shape[1],), dtype=X_train.dtype,
name='main_input')
xx = Embedding(output_dim=512, input_dim=3000, input_length=len(X))
(main_input)
xx= SpatialDropout1D(0.4)(xx)
lstm_out = LSTM(64)(xx)
#lstm_out = Dense(3,activation='softmax')(lstm_out)
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
auxiliary_input = Input(shape=(Z_train.shape[1],), name='aux_input')
auxB= Input(shape=(hasB_train.shape[1],), name='aux_B')
auxM = Input(shape=(hasM_train.shape[1],), name='aux_M')
auxBM_input = keras.layers.concatenate([ auxB, auxM])
auxiliary_output = Dense(3, activation='softmax', name='aux_output') (lstm_out)
auxBM_output = Dense(3, activation='softmax', name='auxBM_output') (auxBM_input)
x = keras.layers.concatenate([lstm_out, auxiliary_input, auxBM_input])
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
main_output = Dense(3, activation='sigmoid', name='main_output')(x)
model = Model(inputs=[main_input, auxiliary_input, auxB, auxM], outputs= [main_output, auxiliary_output, auxBM_output])
model.compile(optimizer='rmsprop', loss='categorical_crossentropy' ,metrics = ['accuracy'], loss_weights=[4, 1, 10])
model.summary()
when I run the this code model.save('model.h5'), I receive the below error:
Traceback (most recent call last): File
"C:/.../ENV/newDataset/combined3.py", line 209, in
model.save('blah.h5') File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\network.py",
line 1085, in save
save_model(self, filepath, overwrite, include_optimizer) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\saving.py",
line 117, in save_model
}, default=get_json_type).encode('utf8') File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json__init__.py",
line 237, in dumps
**kw).encode(obj) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json\encoder.py",
line 198, in encode
chunks = self.iterencode(o, _one_shot=True) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json\encoder.py",
line 256, in iterencode
return _iterencode(o, 0) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\saving.py",
line 84, in get_json_type
return obj.item() AttributeError: 'numpy.dtype' object has no attribute 'item'
I have no problem, if I run the below code:
model = Sequential()
model.add(Embedding(max_fatures, embed_dim,input_length = X.shape[1]))
model.add(SpatialDropout1D(0.4))
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(3,activation='softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy'])
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,train_size=0.8, random_state = 42)
model.fit(X_train, Y_train, epochs = 1, batch_size=32,shuffle=True)
model.save('test.h5')

I believe you are having this issue because of how Keras handles the dtype argument when you are creating a Functional Model. Keras expects the dtype to be just a simple string and not a numpy.dtype object, and therefore, it will have difficulty saving the model when you pass a numpy object into this argument.
To adjust, I would use one of the strings to describe the data input type, as suggested at https://keras.io/backend/.
I had a similar issue, and when I changed the dtype argument to what Keras was expecting (a string), I was able to save the model without any additional problem.
To fix your issue, I would suggest, changing the dtype=X_train.dtype argument to dtype=X_train.dtype.name, as this would produce the string form of the dtype, which can be handled by Keras.

Related

The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)

I am trying to build a binary Prediction model through Keras TensorFlow.
And I am having trouble when I add data augmentation inside.
This is my code.
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)
train_dir = 'C:/train'
test_dir = 'C:/test'
train_data = train_datagen.flow_from_directory(train_dir,target_size=(224,224),class_mode='binary',seed=42)
test_data = test_datagen.flow_from_directory(test_dir,target_size=(224,224),class_mode='binary',seed=42)
tf.random.set_seed(42)
from tensorflow.keras.layers.experimental import preprocessing
data_augmentation = keras.Sequential([
preprocessing.RandomFlip("horizontal"),
preprocessing.RandomZoom(0.2),
preprocessing.RandomRotation(0.2),
preprocessing.RandomHeight(0.2),
preprocessing.RandomWidth(0.2),
], name='data_augmentation')
model_1 = tf.keras.Sequential([
tf.keras.layers.Input(shape=(224,224,3),name='input_layer'),
data_augmentation,
tf.keras.layers.Conv2D(20,3,activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=2),
tf.keras.layers.Conv2D(20,3,activation='relu'),
tf.keras.layers.MaxPool2D(pool_size=2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1,activation='sigmoid')
])
model_1.compile(loss=tf.keras.losses.binary_crossentropy,
optimizer='Adam',
metrics=['accuracy'])
model_1.fit(train_data,epochs=10,validation_data=test_data)
I ready tried this way but error again
inputs = tf.keras.layers.Input(shape=(224,224,3),name='input_layer')
x = data_augmentation(inputs)
x = tf.keras.layers.Conv2D(20,3,activation='relu')(x)
x = tf.keras.layers.Conv2D(20,3,activation='relu')(x)
x = tf.keras.layers.MaxPool2D(pool_size=2)(x)
x = tf.keras.layers.Flatten()(x)
outputs = tf.keras.layers.Dense(1,activation='sigmoid')(x)
model_1 = tf.keras.Model(inputs,outputs)
and this is error message:
Traceback (most recent call last):
File "C:/Users/pondy/PycharmProjects/pythonProject2/main.py", line 60, in <module>
model_1 = tf.keras.Sequential([
File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\tensorflow\python\training\tracking\base.py", line 530, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\pondy\PycharmProjects\pythonProject2\venv\lib\site-packages\keras\layers\core\dense.py", line 139, in build
raise ValueError('The last dimension of the inputs to a Dense layer '
ValueError: The last dimension of the inputs to a Dense layer should be defined. Found None. Full input shape received: (None, None)
Process finished with exit code 1
if didn't add data_augmentation inside it's not error
thank you for help<3
your code will work if you remove
preprocessing.RandomHeight(0.2),
preprocessing.RandomWidth(0.2),

convLSTM2d w/ functional API

I have an autoencoder for image compression, where the encoded tensor has the shape: (batch_size, 12, 64, 48).
batch_size is the number of images being fed in a batch,
12 is the number of channels of this last encoder layer, which has a
64x48 width/height.
I want to input this to a ConvLSTM2D layer, and i would like the output of the ConvLSTM2D to have the same dimension as the input of the ConvLSTM2D.
The intention is to see image reconstruction on a video sequence, rather than unordered images from a dataset.
Placing a ConvLSTM2d between an encoder/decoder in a autoencoder architecture has been difficult, especially because most examples use the Sequential API, and i want to use the functional API in Keras.
I tried reshaping the input but the error persists
import tensorflow as tf
import tensorflow.keras.backend as K
def LSTM_layer(input):
input = tf.keras.backend.expand_dims(input, axis=-1)
lstm1 = tf.keras.layers.ConvLSTM2D(filters=12, kernel_size=(3, 3), strides=(1, 1), data_format="channels_first",
input_shape=(None, 12, 64, 48),
padding='same', return_sequences=True)(input)
return lstm1
def build_model(input_shape):
#create an input with input_shape as the size
input_ = tf.keras.Input(shape=input_shape, name="input_node")
lstm_features = LSTM_layer(input_)
model = tf.keras.Model(inputs=input_, outputs=[lstm_features])
return model
def main():
input_shape = (12, 64, 48) #this is the size of the tensor which is outputted by my encoder, with channels_first assumed
model = build_model(input_shape)
if __name__ == '__main__':
main()
Unfortunately, this is throwing this error:
Traceback (most recent call last):
File "lstm.py", line 29, in <module>
main()
File "lstm.py", line 26, in main
model = build_model(input_shape)
File "lstm.py", line 20, in build_model
model = tf.keras.Model(inputs=input_, outputs=[lstm_features])
File "/home/hallab/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 121, in __init__
super(Model, self).__init__(*args, **kwargs)
File "/home/hallab/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py", line 80, in __init__
self._init_graph_network(*args, **kwargs)
File "/home/hallab/.local/lib/python3.5/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
method(self, *args, **kwargs)
File "/home/hallab/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py", line 224, in _init_graph_network
'(thus holding past layer metadata). Found: ' + str(x))
ValueError: Output tensors to a Model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: Tensor("conv_lst_m2d/transpose_1:0", shape=(?, 12, 12, 48, 1), dtype=float32)
Most posts about this error instruct to wrap the operation in a lambda.. but i am not implementing a custom operation here, this should be a keras tf layer... right?
Also, in my implementation, i want the output tensor from the LSTM unit to be the same as the input, can i get some feedback about that as well?
Thank you.
You could use Lambda to wrap the output form K.expand_dims before input it to next layer like this:
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Lambda
def expand_dims(x):
return K.expand_dims(x, 1)
def expand_dims_output_shape(input_shape):
return (input_shape[0], 1, input_shape[1])
def LSTM_layer(input_):
lstm1 = Lambda(expand_dims, expand_dims_output_shape)(input_)
lstm1 = tf.keras.layers.ConvLSTM2D(filters=12, kernel_size=(3, 3), strides=(1, 1), data_format="channels_first", padding='same', return_sequences=False)(lstm1)
return lstm1

model.fit on keras.sequential when using tf.data.Dataset raises a ValueError

I am trying to build my first classifier on tensorflow 1.10 using tf.data.dataset as an input to a Keras.sequential but the fit method returns the following error:
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (None,)
First I initialized 2 tf.data.Dataset with the filenames of my dataset
#Initialize dataset directories location and parameters
image_size=50
batch_size=10
mortys_file_pattern = r'C:\Users\Jonas\Downloads\mortys\*'
ricks_file_pattern = r'C:\Users\Jonas\Downloads\ricks\*'
#Each tensor in those dataset will be a filename for a specific image
mortys_dataset = tf.data.Dataset.list_files(mortys_file_pattern)
ricks_dataset = tf.data.Dataset.list_files(ricks_file_pattern)
Then I used the map method to prepare my datasets
#Now, each dataset entry will contain 2 tensors: image,label
mortys_dataset.map(lambda filename: load_resize_label(filename, "morty"))
ricks_dataset.map(lambda filename: load_resize_label(filename, "rick"))
def load_resize_label(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string)
image_resized = tf.image.resize_images(image_decoded, [image_size, image_size])
image_resized=image_resized/255.0
return image_resized, tf.convert_to_tensor(label)
Then, I concatenate the datasets into one final dataset and initialize the batch size
#Merge the datasets
dataset = mortys_dataset.concatenate(ricks_dataset)
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
In the end, use the compile and fit method of the model object
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(dataset, epochs=10, steps_per_epoch=30)
(Full code bellow)
I'm using:
Windows 10 64bits
cudnn-9.0-windows10-x64-v7.2.1.38
cuda_9.0.176_win10
tensorflow-gpu 1.10.0
import tensorflow as tf
from tensorflow import keras
image_size=50
batch_size=10
# Reads an image from a file, decodes it into a dense tensor, resizes it
# to a fixed shape.
def load_resize_label(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string)
image_resized = tf.image.resize_images(image_decoded, [image_size, image_size])
image_resized=image_resized/255.0
return image_resized, tf.convert_to_tensor(label)
#Initialize dataset directories location
mortys_file_pattern = r'C:\Users\Jonas\Downloads\mortys\*'
ricks_file_pattern = r'C:\Users\Jonas\Downloads\ricks\*'
#Each tensor in those dataset will be a filename for a specific image
mortys_dataset = tf.data.Dataset.list_files(mortys_file_pattern)
ricks_dataset = tf.data.Dataset.list_files(ricks_file_pattern)
#Now, each dataset entry will contain 2 tensors: image,label
mortys_dataset = mortys_dataset.map(lambda filename: load_resize_label(filename, "morty"))
ricks_dataset = ricks_dataset.map(lambda filename: load_resize_label(filename, "rick"))
#Merge the datasets
dataset = mortys_dataset.concatenate(ricks_dataset)
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
#the CNN architecture
model = keras.Sequential([
keras.layers.Convolution2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(image_size, image_size,3)),
keras.layers.MaxPool2D(pool_size=2),
keras.layers.BatchNormalization(),
keras.layers.Flatten(),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dropout(0.3),
keras.layers.Dense(2, activation=tf.nn.softmax)
])
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(dataset, epochs=10, steps_per_epoch=30)
Traceback:
Traceback (most recent call last):
File "C:/Users/Jonas/PycharmProjects/learning/lesson2.py", line 47, in <module>
model.fit(dataset, epochs=10, steps_per_epoch=30)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1278, in fit
validation_split=validation_split)
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training.py", line 917, in _standardize_user_data
exception_prefix='target')
File "C:\Users\Jonas\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\training_utils.py", line 182, in standardize_input_data
'with shape ' + str(data_shape))
ValueError: Error when checking target: expected dense_1 to have 2 dimensions, but got array with shape (None,)
You're missing some '=' in your code.
Each dataset operation should be like :
dataset = dataset.some_ops(...)
Here is how your code should look:
import tensorflow as tf
from tensorflow import keras
image_size=50
batch_size=10
# Reads an image from a file, decodes it into a dense tensor, resizes it
# to a fixed shape.
def load_resize_label(filename, label):
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_jpeg(image_string)
image_resized = tf.image.resize_images(image_decoded, [image_size, image_size])
image_resized=image_resized/255.0
if label == 'morty':
label = [0, 1]
elif label == 'rick':
label = [1, 0]
else:
raise ValueError(label)
return image_resized, tf.convert_to_tensor(label)
#Initialize dataset directories location
mortys_file_pattern = r'C:\Users\Jonas\Downloads\mortys\*'
ricks_file_pattern = r'C:\Users\Jonas\Downloads\ricks\*'
#Each tensor in those dataset will be a filename for a specific image
mortys_dataset = tf.data.Dataset.list_files(mortys_file_pattern)
ricks_dataset = tf.data.Dataset.list_files(ricks_file_pattern)
#Now, each dataset entry will contain 2 tensors: image,label
mortys_dataset = mortys_dataset.map(lambda filename: load_resize_label(filename, "morty"))
ricks_dataset = ricks_dataset.map(lambda filename: load_resize_label(filename, "rick"))
#Merge the datasets
dataset = mortys_dataset.concatenate(ricks_dataset)
dataset = dataset.batch(batch_size)
dataset = dataset.repeat()
#the CNN architecture
model = keras.Sequential([
keras.layers.Convolution2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(image_size, image_size, 3)),
keras.layers.MaxPool2D(pool_size=2),
keras.layers.BatchNormalization(),
keras.layers.Flatten(),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dropout(0.3),
keras.layers.Dense(2, activation=tf.nn.softmax)
])
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(dataset, epochs=10, steps_per_epoch=30)
Also, I advise you to use dataset.prefetch(None) and use the num_parallel_calls argument in the map function. Here is why. TLDR: it's faster.

Keras: Bug that depends on how many layers in network

I am having trouble with a seemingly arbitrary bug when using keras. I run into the error "NotFoundError: FeedInputs: unable to find feed output dense_3_target:0" when trying to build a model in keras. The error seems to depend on the number of layers I put in the network (bug when number of layers not equal to 4). Does anyone know what is going on here?
The code and error message:
import tensorflow as tf
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense
tf.reset_default_graph()
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist", one_hot=True)
X_train = mnist.train.images
y_train = mnist.train.labels
X_test = mnist.test.images
y_test = mnist.test.labels
# Hyper Parameters
n_features = 784
n_classes = 10
learning_rate = 0.5
training_epochs = 2
model = Sequential()
model.add(Dense(units = 100, activation = 'relu', input_dim = n_features))
model.add(Dense(units = 50,activation = 'relu'))
model.add(Dense(50,activation = 'relu'))
model.add(Dense(units = n_classes, activation = 'softmax'))
# Step 3: Compile the Model
model.compile(optimizer='adam',loss='categorical_crossentropy')
## Step 4: Train the Model
history = model.fit(X_train,y_train,epochs=10,batch_size = 100,validation_data=(X_test,y_test))
===================================================================
File "<ipython-input-14-1076cda88cc6>", line 43, in <module>
history = model.fit(X_train,y_train,epochs=10,batch_size = 100,validation_data=(X_test,y_test))
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/engine/training.py", line 1037, in fit
validation_steps=validation_steps)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 199, in fit_loop
outs = f(ins_batch)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2666, in __call__
return self._call(inputs)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2635, in _call
session)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2587, in _make_callable
callable_fn = session._make_callable_from_options(callable_opts)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1480, in _make_callable_from_options
return BaseSession._Callable(self, callable_options)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1441, in __init__
session._session, options_ptr, status)
File "/Users/liyuan/anaconda2/envs/tensorflow35/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 519, in __exit__
c_api.TF_GetCode(self.status.status))
NotFoundError: FeedInputs: unable to find feed output dense_3_target:0

How to pass `None` Batch Size to tensorflow dynamic rnn?

I am trying to build a CNN+LSTM+CTC model for word recognition.
Initially I have an image, I am converting the features extracted using CNN on word image and building a sequence of features which I am passing as sequential data to RNN.
Follwing is how I am converting the features to sequential data:
[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]] -> [[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
Where a,b,c are 3 features extracted using CNN.
Presently I can pass a constant batch_size to the model common.BATCH_SIZE, but what I want is to be able to pass variable batch_size to the model.
How can this be done ?
inputs = tf.placeholder(tf.float32, [common.BATCH_SIZE, common.OUTPUT_SHAPE[1], common.OUTPUT_SHAPE[0], 1])
# Here we use sparse_placeholder that will generate a
# SparseTensor required by ctc_loss op.
targets = tf.sparse_placeholder(tf.int32)
# 1d array of size [batch_size]
seq_len = tf.placeholder(tf.int32, [common.BATCH_SIZE])
model = tf.layers.conv2d(inputs, 64, (3,3),strides=(1, 1), padding='same', name='c1')
model = tf.layers.max_pooling2d(model, (3,3), strides=(2,2), padding='same', name='m1')
model = tf.layers.conv2d(model, 128,(3,3), strides=(1, 1), padding='same', name='c2')
model = tf.layers.max_pooling2d(model, (3,3),strides=(2,2), padding='same', name='m2')
model = tf.transpose(model, [3,0,1,2])
shape = model.get_shape().as_list()
model = tf.reshape(model, [shape[0],-1,shape[2]*shape[3]])
cell = tf.nn.rnn_cell.LSTMCell(common.num_hidden, state_is_tuple=True)
cell = tf.nn.rnn_cell.DropoutWrapper(cell, input_keep_prob=0.5, output_keep_prob=0.5)
stack = tf.nn.rnn_cell.MultiRNNCell([cell]*common.num_layers, state_is_tuple=True)
outputs, _ = tf.nn.dynamic_rnn(cell, model, seq_len, dtype=tf.float32,time_major=True)
UPDATE:
batch_size = tf.placeholder(tf.int32, None, name='batch_size')
inputs = tf.placeholder(tf.float32, [batch_size, common.OUTPUT_SHAPE[1], common.OUTPUT_SHAPE[0], 1])
# Here we use sparse_placeholder that will generate a
# SparseTensor required by ctc_loss op.
targets = tf.sparse_placeholder(tf.int32)
# 1d array of size [batch_size]
seq_len = tf.placeholder(tf.int32, [batch_size])
model = tf.layers.conv2d(inputs, 64, (3,3),strides=(1, 1), padding='same', name='c1')
model = tf.layers.max_pooling2d(model, (3,3), strides=(2,2), padding='same', name='m1')
model = tf.layers.conv2d(model, 128,(3,3), strides=(1, 1), padding='same', name='c2')
model = tf.layers.max_pooling2d(model, (3,3),strides=(2,2), padding='same', name='m2')
model = tf.transpose(model, [3,0,1,2])
shape = model.get_shape().as_list()
model = tf.reshape(model, [shape[0],-1,shape[2]*shape[3]])
cell = tf.nn.rnn_cell.LSTMCell(common.num_hidden, state_is_tuple=True)
cell = tf.nn.rnn_cell.DropoutWrapper(cell, input_keep_prob=0.5, output_keep_prob=0.5)
stack = tf.nn.rnn_cell.MultiRNNCell([cell]*common.num_layers, state_is_tuple=True)
outputs, _ = tf.nn.dynamic_rnn(cell, model, seq_len, dtype=tf.float32,time_major=True)
I am getting error as below:
Traceback (most recent call last):
File "lstm_and_ctc_ocr_train.py", line 203, in <module>
train()
File "lstm_and_ctc_ocr_train.py", line 77, in train
logits, inputs, targets, seq_len, batch_size = model.get_train_model()
File "/home/himanshu/learning-tf/tf/code/tensorflow_lstm_ctc_ocr/model.py", line 20, in get_train_model
inputs = tf.placeholder(tf.float32, [batch_size, common.OUTPUT_SHAPE[1], common.OUTPUT_SHAPE[0], 1])
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1530, in placeholder
return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1954, in _placeholder
name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 705, in apply_op
attr_value.shape.CopyFrom(_MakeShape(value, key))
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 198, in _MakeShape
return tensor_shape.as_shape(v).as_proto()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 798, in as_shape
return TensorShape(shape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 434, in __init__
self._dims = [as_dimension(d) for d in dims_iter]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 376, in as_dimension
return Dimension(value)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 32, in __init__
self._value = int(value)
TypeError: int() argument must be a string or a number, not 'Tensor'
You should be able to pass batch_size to dynamic RNN as a placeholder. In my experience, the only headache you might experience is if you don't specify its shape in advance, so you should pass [] to make things work, something like this:
batchsize = tf.placeholder(tf.int32, [], name='batchsize')
and then feed its value during sess.run() in the usual way. This works well for me while training on a large batch size, but then generating with batch of 1.
But strictly speaking, you don't even need to specify batch size for dynamic_rnn specifically, do you? You need it if you use MultiRNNCell to get zero state, but I don't see you doing that in your code...
*** UPDATE:
As discussed in the comment, your problem seems to have nothing to do with dynamic_rnn and more to do with the fact that you are using a placeholder inputs to specify the shape of another placeholder seq_len. Here's the code that reproduces the same error:
import tensorflow as tf
a = tf.placeholder(tf.int32, None, name='a')
b = tf.placeholder(tf.int32, [a, 5], name='b')
c = b * 5
with tf.Session() as sess:
C = sess.run(c, feed_dict={a:1, b:[[1,2,3,4,5]]})
And here's the error:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'
Before getting to the headache of dynamic_rnn, I would recommend finding a way around this, either by changing your code or by asking a separate question on how to fudge it with placeholders.