[英]Multiple Inputs with different sample size in Neural Networks
I am working on a specific neural network which gets two different inputs:我正在研究一个特定的神经网络,它获得两个不同的输入:
When I define and run the model it as to below当我定义并运行模型时,它如下
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)
............
After running the model,运行模型后,
vae.fit([X_train,A_vec], outputs,shuffle=True, batch_size=m, epochs=n_epoch)
I get this error:我收到此错误:
ValueError: All input arrays (x) should have the same number of samples.
ValueError:所有输入数组 (x) 应具有相同数量的样本。 Got array shapes: [(50000, 784), TensorShape([Dimension(28)])]
得到数组形状:[(50000, 784), TensorShape([Dimension(28)])]
It means my inputs have different sizes.这意味着我的输入有不同的大小。 How can I use differetn inputs when they have different sizes (or shapes)?
当它们具有不同的大小(或形状)时,如何使用不同的输入?
The inputs have to have the same size, eg (50000, 748) and (50000, 28), ie one per sample.输入必须具有相同的大小,例如 (50000, 748) 和 (50000, 28),即每个样本一个。 Try create a numpy array size (50000, 28) for
A_vec
: numpy.random.normal(0., 1.0, (50000, 28)
.尝试为
A_vec
创建一个 numpy 数组大小 (50000, 28) : numpy.random.normal(0., 1.0, (50000, 28)
。
Or if you want the same vector for all, create it and repeat 50000 times.或者,如果您希望所有人都使用相同的向量,请创建它并重复 50000 次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.