簡體   English   中英

如何從我的 Keras 神經網絡一次做出一個預測

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

我在 Tensorflow 2.2.0 中制作了一個 GAN,並且在我的縮減數據集(48 個樣本)上取得了進展。 為了克服我現在在鑒別器上看到的一些問題,我決定是時候開始使用包含 1400 個樣本的完整數據集了。 每個是 3 個特征 (4000,3) 的 4000 個時間步長。

經過大量的努力和搜索,我終於開始掌握batch_sizeinput_shape之間的區別。 真正有幫助的是將下面的代碼從batch_size改寫為shape並看到它的工作原理是一樣的。

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'])

但是,我仍然必須錯過 Tensorflow 模型中batch_sizeinput_shape如何協同工作的一部分。 目前,如果我傳遞一個與我減少的訓練數據集大小相同的隨機種子數組,我只能預測合成數據。 而我的印象是,一旦我訓練了 GAN,我就可以使用生成器進行任何大小的單獨預測。 這個規模問題是相關的,因為一次只能預測 1400 個樣本是不切實際的。 我已經很好地查看了 Tensorflow 文檔中的Model 頁面,關於如何以簡單的方式完成此操作,我並沒有什么特別突出的地方。

#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)

用一個隨機樣本為生成器播種會返回與數據的預期維度相關的各種錯誤。 生成器期望 [None,48] 但被輸入 [None,1] 從而返回錯誤。

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]

我猜我對batch_sizeinput_shape如何相互關聯的知識仍然存在差距。 任何關於如何完成的建議示例將不勝感激。

seed[0]是一個形狀為 (n_features,) 的張量。 您需要向生成器傳遞一個形狀為 (batch_dim, n_features) 的張量,在單個樣本的情況下為 (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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM