mnick / scikit-tensor

Python library for multilinear algebra and tensor factorizations
GNU General Public License v3.0
401 stars 113 forks source link

Batch_size problem in using ttm in a custom layer in keras #42

Open sandeeppandey456 opened 5 years ago

sandeeppandey456 commented 5 years ago

I have a custom layer in keras where the input is a 2D tensor, but the layer takes a batch of those and the shape it gets is (?, 61,80). The '?' part is for the batch_size . While using the ttm function of sktensor I am getting the error . please help.

The custom layer is

 def call(self, inputs):

num, n, m = inputs.shape 
print(inputs.shape)
(k1,k2) = self.output_dim
input_tensor = inputs 

print(input_tensor)

input_tensor = dtensor(input_tensor)
kernel1=np.array(self.W1)
kernel2=np.array(self.W2)

feed_forward_product=input_tensor.ttm([kernel1, kernel2],mode=0, transp=False, without=True)

feed_forward_product = np.array(feed_forward_product )
result = K.tanh(feed_forward_product)

return result

Variables W1 and W2 are declared beforehand. The error I am getting is-

x_train shape: (269, 61, 80) 269 train samples 70 test samples <tf.Variable 'neural_tensor_layer_2/W1:0' shape=(40, 61) dtype=float32_ref> <tf.Variable 'neural_tensor_layer_2/W2:0' shape=(40, 80) dtype=float32_ref> (?, 61, 80) Tensor("sequential_2_input:0", shape=(?, 61, 80), dtype=float32) Traceback (most recent call last):

File "", line 1, in runfile('/home/hanumant/Documents/December_experiments/python_codes/TFNN_v2.py', wdir='/home/hanumant/Documents/December_experiments/python_codes')

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile execfile(filename, namespace)

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "/home/hanumant/Documents/December_experiments/python_codes/TFNN_v2.py", line 79, in validation_data=(np.array(x_test),np.array(y_test)))

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/keras/engine/training.py", line 952, in fit batch_size=batch_size)

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/keras/engine/training.py", line 677, in _standardize_user_data self._set_inputs(x)

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/keras/engine/training.py", line 589, in _set_inputs self.build(input_shape=(None,) + inputs.shape[1:])

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/keras/engine/sequential.py", line 221, in build x = layer(x)

File "/home/hanumant/.conda/envs/myenv/lib/python3.5/site-packages/keras/engine/base_layer.py", line 457, in call output = self.call(inputs, **kwargs)

File "/home/hanumant/Documents/December_experiments/python_codes/Neural_Tensor_layer.py", line 138, in call feed_forward_product=input_tensor.ttm([kernel1, kernel2],mode=0, transp=False, without=True)

File "/home/hanumant/Downloads/scikit-tensor-master/scikit-tensor/sktensor/core.py", line 99, in ttm dims, vidx = check_multiplication_dims(mode, self.ndim, len(V), vidx=True, without=without)

File "/home/hanumant/Downloads/scikit-tensor-master/scikit-tensor/sktensor/core.py", line 256, in check_multiplication_dims raise ValueError('More multiplicants than dimensions')

ValueError: More multiplicants than dimensions

please help