Data Science Asked on July 11, 2021
I want to implement a custom RNN/LSTM model similar to this. The model should take two separate vectors as input and process them. I was following keras tutorial to implement a custom keras layer and inputting two vectors a and b as a list [a,b] to the layer as shown below.
import keras
from keras.layers.recurrent import RNN
import keras.backend as K
class MinimalRNNCell(keras.layers.Layer):
def __init__(self, units, **kwargs):
self.units = units
self.state_size = units
super(MinimalRNNCell, self).__init__(**kwargs)
def build(self, input_shape):
print(type(input_shape))
self.kernel = self.add_weight(shape=(input_shape[0][-1], self.units),
initializer='uniform',
name='kernel')
self.recurrent_kernel = self.add_weight(
shape=(self.units, self.units),
initializer='uniform',
name='recurrent_kernel')
self.built = True
def call(self, inputs, states):
prev_output = states[0]
h = K.dot(inputs[0], self.kernel)
output = h + K.dot(prev_output, self.recurrent_kernel)
return output, [output]
# Let's use this cell in a RNN layer:
cell = MinimalRNNCell(32)
a = keras.Input((None, 5))
b = keras.Input((None, 5))
layer = RNN(cell)
y = layer([a,b])
But I am getting the error TypeError: 'NoneType' object has no attribute '__getitem__'
at
self.kernel = self.add_weight(shape=(input_shape[0][-1], self.units),
initializer='uniform',
name='kernel')
Also the type of the input_shape is showing as <type 'tuple'>
, not a list.
Whats wrong I am doing and how to overcome this error. Please help
I think, input_shape[0]
is None
. Based on my understanding, you will pass a matrix of size (Batch_size, Time steps, dim of each time step data) to an RNN cell. And while creating your architecture, input_shape[0]
will give the batch size (if you mentioned what is the batch size in input layer, if not you will get None
). Even if you mention batch size, it will be an integer and you can't index the integers so you are getting that error.
Answered by Uday on July 11, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP