Skip to content

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

@laurilaatu

Description

@laurilaatu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions