google / google/qkeras

QGRU Error when compiling with input features not the same as units

Open
#72 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
584
Forks
108
PR merge metrics
No merged PRs in 30d

Description

Building a simple GRU model using Keras:
```
gru = Sequential(GRU(16, input_shape=(2,4)))
gru.compile(loss='mse', optimizer='adam')
gru.summary()
```

Produces output:

```
Model: "sequential_332"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
gru_3 (GRU) (None, 16) 1056
=================================================================
Total params: 1,056
Trainable params: 1,056
Non-trainable params: 0
```

Trying to build same model with `QGRU` layer:

```
gru = Sequential(QGRU(16, input_shape=(2,4)))
gru.compile(loss='mse', optimizer='adam')
gru.summary()
```
Produces the following error:

```
ValueError: in user code:

/lib/python3.7/site-packages/qkeras/qrecurrent.py:1304 call *
inputs, mask=mask, training=training, initial_state=initial_state)
/lib/python3.7/site-packages/qkeras/qrecurrent.py:1129 call *
recurrent_z = K.dot(h_tm1_z, quantized_recurrent[:, :self.units])
/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper **
return target(*args, **kwargs)
/lib/python3.7/site-packages/tensorflow/python/keras/backend.py:1898 dot
out = math_ops.matmul(x, y)
/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3315 matmul
a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:5550 mat_mul
name=name)
/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:750 _apply_op_helper
attrs=attr_protos, op_def=op_def)
/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:592 _create_op_internal
compute_device)
/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:3536 _create_op_internal
op_def=op_def)
/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:2016 __init__
control_input_ops, op_def)
/lib/python3.7/site-packages/tensorflow/python/framework/ops.py:1856 _create_c_op
raise ValueError(str(e))

ValueError: Dimensions must be equal, but are 16 and 4 for '{{node qgru_13/qgru_cell_13/MatMul_3}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](qgru_13/zeros, qgru_13/qgru_cell_13/strided_slice_6)' with input shapes: [?,16], [4,16].

```

Setting `units` equal to input features produces no error. The issue is only present in `QGRU`. Other recurrent layers `QLSTM` or `QSimpleRNN` do not have this issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.