def cifar10_test():
    dtype_1 = np.complex64
    (train_images, train_labels), (test_images,
                                   test_labels) = datasets.cifar10.load_data()
    # Normalize pixel values to be between 0 and 1
    train_images, test_images = train_images / 255.0, test_images / 255.0
    train_images = train_images.astype(dtype_1)
    test_images = test_images.astype(dtype_1)
    train_labels = train_labels.astype(dtype_1)
    test_labels = test_labels.astype(dtype_1)

    model = models.Sequential()
    model.add(layers.ComplexInput(input_shape=(32, 32, 3),
                                  dtype=dtype_1))  # Never forget this!!!
    model.add(layers.ComplexConv2D(32, (3, 3), activation='cart_relu'))
    model.add(layers.ComplexMaxPooling2D(
        (2, 2)))  # TODO: This is changing the dtype!
    model.add(layers.ComplexConv2D(64, (3, 3), activation='cart_relu'))
    model.add(layers.ComplexAvgPooling2D((2, 2)))
    model.add(layers.ComplexConv2D(64, (3, 3), activation='cart_relu'))
    model.add(layers.ComplexFlatten())
    model.add(layers.ComplexDense(64, activation='cart_relu'))
    model.add(layers.ComplexDense(10, activation='convert_to_real_with_abs'))
    model.summary()
    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=['accuracy'])
    history = model.fit(train_images,
                        train_labels,
                        epochs=2,
                        validation_data=(test_images, test_labels))
Пример #2
0
def cifar10_test_model_1(train_images,
                         train_labels,
                         test_images,
                         test_labels,
                         dtype_1='complex64'):
    model = models.Sequential()
    model.add(layers.ComplexInput(input_shape=(32, 32, 3),
                                  dtype=dtype_1))  # Never forget this!!!
    model.add(layers.ComplexConv2D(32, (3, 3), activation='cart_relu'))
    model.add(layers.ComplexMaxPooling2D((2, 2)))
    model.add(layers.ComplexConv2D(64, (3, 3), activation='cart_relu'))
    model.add(layers.ComplexAvgPooling2D((2, 2)))
    model.add(layers.ComplexConv2D(64, (3, 3), activation='cart_relu'))
    model.add(layers.ComplexFlatten())
    model.add(layers.ComplexDense(64, activation='cart_relu'))
    model.add(layers.ComplexDense(10, activation='convert_to_real_with_abs'))
    model.summary()
    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=['accuracy'])
    return model.fit(train_images,
                     train_labels,
                     epochs=2,
                     validation_data=(test_images, test_labels),
                     shuffle=False)
Пример #3
0
def cifar10_test_model_2(train_images,
                         train_labels,
                         test_images,
                         test_labels,
                         dtype_1='complex64'):
    x = layers.complex_input(shape=(32, 32, 3), dtype=dtype_1)
    conv1 = layers.ComplexConv2D(32, (3, 3), activation='cart_relu')(x)
    pool1 = layers.ComplexMaxPooling2D((2, 2))(conv1)
    conv2 = layers.ComplexConv2D(64, (3, 3), activation='cart_relu')(pool1)
    pool2 = layers.ComplexAvgPooling2D((2, 2))(conv2)
    conv3 = layers.ComplexConv2D(64, (3, 3), activation='cart_relu')(pool2)
    flat = layers.ComplexFlatten()(conv3)
    dense1 = layers.ComplexDense(64, activation='cart_relu')(flat)
    y = layers.ComplexDense(10, activation='convert_to_real_with_abs')(dense1)

    model = models.Model(inputs=[x], outputs=[y])
    model.compile(
        optimizer='adam',
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=['accuracy'])
    model.summary()
    return model.fit(train_images,
                     train_labels,
                     epochs=2,
                     validation_data=(test_images, test_labels),
                     shuffle=False)
Пример #4
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
Пример #5
0
def test_cifar():
    (train_images, train_labels), (test_images, test_labels) = get_dataset()

    # Create your model
    model = tf.keras.models.Sequential()
    model.add(complex_layers.ComplexInput(input_shape=(32, 32, 3)))  # 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_relu'))
    model.add(complex_layers.ComplexFlatten())
    model.add(complex_layers.ComplexDense(64, activation='cart_relu'))
    model.add(complex_layers.ComplexDense(10, activation='convert_to_real_with_abs'))
    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    # model.summary()
    history = model.fit(train_images, train_labels, epochs=1, validation_data=(test_images, test_labels))
    test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)