Пример #1
0
def random_dataset():
    x_train = np.complex64(tf.complex(tf.random.uniform([640, 65, 82, 1]), tf.random.uniform([640, 65, 82, 1])))
    x_test = np.complex64(tf.complex(tf.random.uniform([200, 65, 82, 1]), tf.random.uniform([200, 65, 82, 1])))
    y_train = np.uint8(np.random.randint(5, size=(640, 1)))
    y_test = np.uint8(np.random.randint(5, size=(200, 1)))

    model = tf.keras.models.Sequential()
    model.add(complex_layers.ComplexInput(input_shape=(65, 82, 1)))  # Always use ComplexInput at the start
    model.add(complex_layers.ComplexConv2D(8, (5, 5), activation='cart_relu'))
    model.add(complex_layers.ComplexMaxPooling2D((2, 2)))
    model.add(complex_layers.ComplexConv2D(16, (5, 5), activation='cart_relu'))
    model.add(complex_layers.ComplexFlatten())
    model.add(complex_layers.ComplexDense(256, activation='cart_relu'))
    model.add(complex_layers.ComplexDropout(0.1))
    model.add(complex_layers.ComplexDense(64, activation='cart_relu'))
    model.add(complex_layers.ComplexDropout(0.1))
    model.add(complex_layers.ComplexDense(5, activation='convert_to_real_with_abs'))
    # An activation that casts to real must be used at the last layer.
    # The loss function cannot minimize a complex number

    # Compile it
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'],
                  # run_eagerly=Trutest_regressione
                  )
    model.summary()
    # Train and evaluate
    history = model.fit(x_train, y_train, epochs=2, validation_data=(x_test, y_test))
    test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
Пример #2
0
def all_layers_model():
    """
    Creates a model using all possible layers to assert no layer changes the dtype to real.
    """
    input_shape = (4, 28, 28, 3)
    x = tf.cast(tf.random.normal(input_shape), tf.complex64)

    model = tf.keras.models.Sequential()
    model.add(complex_layers.ComplexInput(
        input_shape=input_shape[1:]))  # Always use ComplexInput at the start
    model.add(complex_layers.ComplexConv2D(32, (3, 3), activation='cart_relu'))
    model.add(complex_layers.ComplexAvgPooling2D((2, 2)))
    model.add(
        complex_layers.ComplexConv2D(64, (3, 3), activation='cart_sigmoid'))
    model.add(complex_layers.ComplexDropout(0.5))
    model.add(complex_layers.ComplexMaxPooling2D((2, 2)))
    model.add(complex_layers.ComplexConv2DTranspose(32, (2, 2)))
    model.add(complex_layers.ComplexFlatten())
    model.add(complex_layers.ComplexDense(64, activation='cart_tanh'))
    model.compile(loss=tf.keras.losses.MeanAbsoluteError(),
                  optimizer='adam',
                  metrics=['accuracy'])
    y = model(x)
    assert y.dtype == np.complex64
    return model
Пример #3
0
 def test_complex_dropout_output(self):
     dropout = layers.ComplexDropout(self.dropout)
     x = np.exp(1j *
                np.random.uniform(-np.pi, np.pi, size=(1, self.input_dim)))
     y = dropout(x, training=True)
     self.assertTrue(np.sum(np.real(y.numpy()) == 0.) > 0)
     self.assertTrue(np.sum(np.imag(y.numpy()) == 0.) > 0)
     self.assertEqual(y.numpy().shape, (1, self.input_dim))
     self.assertTrue(
         np.issubdtype(y.dtype.as_numpy_dtype, np.complexfloating))
Пример #4
0
def get_complex_mnist_model():
    inputs = complex_layers.complex_input(shape=(28, 28, 1), dtype=np.float32)
    flat = complex_layers.ComplexFlatten(input_shape=(28, 28, 1), dtype=np.float32)(inputs)
    dense = complex_layers.ComplexDense(128, activation='cart_relu', dtype=np.float32)(flat)
    drop = complex_layers.ComplexDropout(rate=0.5)(dense)
    out = complex_layers.ComplexDense(10, activation='softmax_real_with_abs', dtype=np.float32)(drop)
    complex_model = tf.keras.Model(inputs, out, name="rvnn")
    complex_model.compile(
        loss='sparse_categorical_crossentropy',
        optimizer=tf.keras.optimizers.Adam(0.001),
        metrics=['accuracy'],
    )

    complex_intermediate_model = tf.keras.Model(inputs, drop)
    return complex_model, complex_intermediate_model
Пример #5
0
def dropout():
    tf.random.set_seed(0)
    layer = complex_layers.ComplexDropout(.2, input_shape=(2, ))
    data = np.arange(10).reshape(5, 2).astype(np.float32)
    data = tf.complex(data, data)
    outputs = layer(data, training=True)
    expected_out = np.array([[0. + 0.j, 1.25 + 1.25j],
                             [2.5 + 2.5j, 3.75 + 3.75j],
                             [5. + 5.j, 6.25 + 6.25j],
                             [7.5 + 7.5j, 8.75 + 8.75j],
                             [10. + 10.j, 0. + 0.j]])
    assert np.all(data == layer(data, training=False))
    assert np.all(outputs == expected_out)
    ds_train, ds_test = get_dataset()
    model = tf.keras.models.Sequential([
        complex_layers.ComplexFlatten(input_shape=(28, 28, 1),
                                      dtype=np.float32),
        complex_layers.ComplexDense(128,
                                    activation='cart_relu',
                                    dtype=np.float32),
        complex_layers.ComplexDropout(rate=0.5),
        complex_layers.ComplexDense(10,
                                    activation='softmax_real',
                                    dtype=np.float32)
    ])
    model.compile(
        loss='sparse_categorical_crossentropy',
        optimizer=tf.keras.optimizers.Adam(0.001),
        metrics=['accuracy'],
    )
    model.fit(ds_train,
              epochs=1,
              validation_data=ds_test,
              verbose=False,
              shuffle=False)
    model.evaluate(ds_test, verbose=False)
Пример #6
0
def simple_random_example():
    tf.random.set_seed(0)
    layer = complex_layers.ComplexDropout(.2, input_shape=(2,), seed=0)
    data = np.arange(10).reshape(5, 2).astype(np.float32)
    data = tf.complex(data, data)
    outputs = layer(data, training=True)
    expected_out = np.array([[0. + 0.j, 0. + 0.j],
                             [0. + 0.j, 3.75 + 3.75j],
                             [5. + 5.j, 6.25 + 6.25j],
                             [7.5 + 7.5j, 8.75 + 8.75j],
                             [10. + 10.j, 11.25 + 11.25j]])
    assert np.all(data == layer(data, training=False))
    assert np.all(outputs == expected_out)
    tf.random.set_seed(0)
    layer = tf.keras.layers.Dropout(.2, input_shape=(2,), seed=0)
    real_outputs = layer(tf.math.real(data), training=True)
    assert np.all(real_outputs == tf.math.real(outputs))