gpt4 book ai didi

python - 神经网络中具有不同样本大小的多个输入

转载 作者:太空宇宙 更新时间:2023-11-04 01:53:33 25 4
gpt4 key购买 nike

我正在研究一个特定的神经网络,它获得两个不同的输入:

  • MNIST数据集,训练集是一个[50000,784]张量
  • 带有 TensorShape([Dimension(28)])] 的辅助向量

当我定义并运行模型时,如下所示

from tensorflow.examples.tutorials.mnist import input_data
from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras.objectives import binary_crossentropy
from keras.callbacks import LearningRateScheduler
import numpy as np
import keras
import matplotlib.pyplot as plt
import keras.backend as K
import tensorflow as tf
from keras.callbacks import LambdaCallback

def load_dataset(flatten=False):
(X_train, y_train), (X_test, y_test) = keras.datasets.mnist.load_data()
# normalize x
X_train = X_train.astype(float) / 255.
X_test = X_test.astype(float) / 255.
# we reserve the last 10000 training examples for validation
X_train, X_val = X_train[:-10000], X_train[-10000:]
y_train, y_val = y_train[:-10000], y_train[-10000:]
if flatten:
X_train = X_train.reshape([X_train.shape[0], -1])
X_val = X_val.reshape([X_val.shape[0], -1])
X_test = X_test.reshape([X_test.shape[0], -1])
return X_train, y_train, X_val, y_val, X_test, y_test
X_train, y_train, X_val, y_val, X_test, y_test = load_dataset(True)

original_dim=784
m = 100 #batchsize
n_z =8
n_epoch = 10
n_d =int(n_z*(n_z - 1 )/2) #or n_d=28

A_vec = K.random_normal(shape=(n_d,), mean=0., stddev=1.)
image_inputs = Input(shape=(784,))
A_inputs = Input(shape=(n_d,))
inputs = keras.layers.concatenate([image_inputs, A_inputs])

h_q1 = Dense(512, activation='relu')(inputs)
h_q2 = Dense(256, activation='relu')(h_q1)
h_q3 = Dense(128, activation='relu')(h_q2)
h_q4= Dense(64, activation='relu')(h_q3)
mu = Dense(n_z, activation='linear')(h_q4)
log_sigma = Dense(n_z, activation='linear')(h_q4)

............

运行模型后,

vae.fit([X_train,A_vec], outputs,shuffle=True, batch_size=m, epochs=n_epoch)

我收到这个错误:

ValueError: All input arrays (x) should have the same number of samples. Got array shapes: [(50000, 784), TensorShape([Dimension(28)])]

这意味着我的输入有不同的大小。当输入具有不同的大小(或形状)时,如何使用不同的输入?

最佳答案

输入必须具有相同的大小,例如(50000, 748) 和 (50000, 28),即每个样本一个。尝试为 A_vec 创建一个 numpy 数组大小 (50000, 28):numpy.random.normal(0., 1.0, (50000, 28)

或者,如果您希望所有人使用相同的矢量,请创建它并重复 50000 次。

关于python - 神经网络中具有不同样本大小的多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57449321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com