Python 如何从我的Keras一次做一个预测';神经网络

Python 如何从我的Keras一次做一个预测';神经网络,python,tensorflow,keras,Python,Tensorflow,Keras,我已经在Tensorflow 2.2.0中做了一个GAN,并且在简化数据集(48个样本)方面取得了进展。为了克服我现在在鉴别器上看到的一些问题,我决定现在开始使用我的1400个样本的完整数据集。每个是3个特征的4000个时间步(4000,3) 经过大量的努力和搜索,我终于开始理解batch\u size和input\u shape之间的区别。真正有帮助的是将下面的代码从batch\u size重写为shape,并确保其工作原理相同 def build_generator(): "

我已经在Tensorflow 2.2.0中做了一个GAN,并且在简化数据集(48个样本)方面取得了进展。为了克服我现在在鉴别器上看到的一些问题,我决定现在开始使用我的1400个样本的完整数据集。每个是3个特征的4000个时间步(4000,3)

经过大量的努力和搜索,我终于开始理解
batch\u size
input\u shape
之间的区别。真正有帮助的是将下面的代码从
batch\u 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'])
然而,我仍然缺少一点,那就是
batch\u size
input\u shape
如何在Tensorflow模型中协同工作。目前,我只能预测合成数据,如果我通过一个随机种子数组,它的大小与我简化的训练数据集的大小相同。而我的印象是,一旦我训练了GAN,我就能够使用生成器进行任何大小的个人预测。这个尺度问题是相关的,因为一次只能预测1400个样本是不现实的。我已经仔细看过Tensorflow文档中的内容,没有任何东西真正让我觉得这是如何以一种直接的方式完成的

#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\u size
input\u shape
如何相互关联方面,我的知识还存在差距。任何关于如何做到这一点的建议都将不胜感激。

seed[0]
是形状张量(n_特征,)。您需要向生成器传递一个形状张量(batch_dim,n_features),对于单个样本,该张量为(1,n_features)


我无法完全理解为什么
[None,:]
会重塑张量,但它的工作原理与预期一致。我以前从未见过python符号<代码>种子[0][None,:]似乎返回一个2D数组。是的,种子[0][None,:]与种子[0]相同。重塑(1,-1)
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)