简体   繁体   中英

How to make a single prediction at a time from my Keras' Neural Net

I have made a GAN in Tensorflow 2.2.0 and have been making progress on my reduced dataset (48 samples). To overcome some of the problems I'm now seeing on the discriminator, I've decided it's time to start using my full dataset of 1400 samples. Each is 4000 time steps of 3 features (4000,3).

After a lot of struggle and searching SO I'm finally starting to grasp the difference between batch_size and input_shape . What really helped was rewriting the below code from batch_size to shape and see that it worked the same.

def build_generator():
    """
    Input is assumed to be uniform random noise in the shape of (training_data.shape[0], 750,)
    """
    generator_input = Input(shape=(750,), name='generator_input')

    x = generator_input

    x = Dense(750, use_bias=True)(x)
    x = BatchNormalization(momentum=0.9)(x)
    x = LeakyReLU()(x)

    x = Reshape( (250,3) )(x)

    x = Conv1DTranspose(128, 3, strides=4, padding="same")(x)
    x = BatchNormalization()(x)
    x = LeakyReLU()(x)

    x = Conv1DTranspose(64, 3, strides=2, padding="same")(x)
    x = BatchNormalization()(x)
    x = LeakyReLU()(x)
  
    x = Conv1DTranspose(32, 3, strides=2, padding="same")(x)
    x = BatchNormalization()(x)
    x = LeakyReLU()(x)

    x = Conv1DTranspose(3, 3, strides=1, padding="same")(x)

    x = Activation('sigmoid')(x)

    generator_output = x

    return Model(generator_input, generator_output)

d = build_discriminator()
g = build_generator()

d.compile(optimizer=SGD(learning_rate=0.0006), loss="binary_crossentropy", metrics=['accuracy'])

model_input = Input(shape=(750,), name='model_input')
model_output = d(g(model_input))
GAN = Model(model_input, model_output)

GAN.compile(optimizer=SGD(learning_rate=0.0005), loss="binary_crossentropy", metrics=['accuracy'])

However, I still must be missing one piece of how batch_size and input_shape work together in Tensorflow models. At the moment I'm only able to predict synthetic data if I pass a random seed array which is the same size as my reduced training dataset. Whereas I was under the impression that once I trained the GAN I would be able to use the generator to make individual predictions of any size. This issue of scale is relevant as it's not practical to be limited to only making predictions 1400 samples at a time. I've had a good look at the Model page in the Tensorflow docs and nothing really sticks out at me as to how this is done in a straightforward manner.

#Reduced dataset length is 48 samples long
seed = tf.random.uniform(
    (48,750,), minval=-1, maxval=1, dtype=tf.dtypes.float32
)

new_samples = g.predict(seed)
new_samples.shape

#returns estimates of the correct shape
(48, 4000, 3)

Seeding the generator with one random sample returns all sorts of errors relating to the expected dimensions of the data. The generator is expecting [None,48] but is fed [None,1] thus returning errors.

g.predict(seed[0])

#Returns the stack trace with the following relevant info
WARNING:tensorflow:Model was constructed with shape (None, 750) for input Tensor("generator_input:0", shape=(None, 750), dtype=float32), but it was called on an input with incompatible shape (None, 1).

ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 750 but received input with shape [None, 1]

I'm guessing there is still a gap in my knowledge as to how batch_size and input_shape relate to each other. Any examples of suggestions as to how this is done would be appreciated.

seed[0] is a tensor of shape (n_features,). You need to pass to the generator a tensor of shape (batch_dim, n_features) that in the case of a single sample is (1,n_features).

seed = tf.random.uniform(
    (48,750,), minval=-1, maxval=1, dtype=tf.dtypes.float32
)

g.predict(seed[0][None,:]) # seed[0][None,:].shape is (1,750)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM