是否可以逐部分训练深度神经网络的输入

我想知道是否可以部分训练神经网络的输入。例如,假设我有输入 256 和输出 256 的神经网络。我要问的是是否有可能采取组,其中每个组仅包含 265 个输入中的 16 个,以便基于单个输入进行预测模型独立训练,然后在最终输出中连接整个组。


例如,提供了以下示例:


from matplotlib import pyplot as plt

import tensorflow as tf


tf.reset_default_graph()


x_train = [[0.,0.],[1.,1.],[1.,0.],[0.,1.]]

y_train = [[0.],[0.],[1.],[1.]]


x_test =  [[0.,0.],[.5,.5],[.5,0.],[0.,.5]]

y_test = [[0.],[0.],[2.],[2.]]


# use placeholder instead so you can have different inputs

x = tf.placeholder('float32', [None, 2])

y = tf.placeholder('float32',)


# Layer 1 = the 2x3 hidden sigmoid

m1 = tf.Variable(tf.random_uniform([2,3], minval=0.1, maxval=0.9, dtype=tf.float32))

b1 = tf.Variable(tf.random_uniform([3], minval=0.1, maxval=0.9, dtype=tf.float32))

h1 = tf.sigmoid(tf.matmul(x, m1) + b1)

# Layer 2 = the 3x1 sigmoid output

m2 = tf.Variable(tf.random_uniform([3,1], minval=0.1, maxval=0.9, dtype=tf.float32))

b2 = tf.Variable(tf.random_uniform([1], minval=0.1, maxval=0.9, dtype=tf.float32))

y_out = tf.sigmoid(tf.matmul(h1, m2) + b2)

### loss

# loss : sum of the squares of y0 - y_out

loss = tf.reduce_sum(tf.square(y - y_out))

# training step : gradient decent (1.0) to minimize loss

train = tf.train.GradientDescentOptimizer(1.0).minimize(loss)


# the two feed dictionaries

feeddict_train = {x: x_train, y: y_train}

feeddict_test = {x: x_test, y: y_test}


### training

# run 500 times using all the X and Y

# print out the loss and any other interesting info

with tf.Session() as sess:

    sess.run(tf.global_variables_initializer())


    train_loss, test_loss = [], []

    for step in range(500):

        loss_train, _ = sess.run([loss, train], feed_dict=feeddict_train)

        train_loss.append(loss_train)


        # under the same tensorflow graph (in the session), use another feed dictionary 

        loss_test = sess.run(loss, feed_dict=feeddict_test)

        test_loss.append(loss_test)


在此命令中,将获取和训练loss_test = sess.run(loss, feed_dict=feeddict_test)整个输入。feeddict_test如果我想把它分成两组,每个组只包含可用的 4 个项目中的 2 个,然后单独测试它们并连接输出,这可能吗?


我怎样才能做到这一点?如果可能的话,你能帮我这样做吗?


手掌心
浏览 123回答 1
1回答

海绵宝宝撒

由于您的问题不准确,您的问题几乎无法解释。第一种解释: 如果您要问的是,如果您的神经网络接收大小为 256 的输入向量并输出大小为 256的向量,那么答案是否定的,您不能输入向量的一部分作为输入并期望它工作。第二种解释: 如果您要问的是,如果您有 256个数据(每个数据是一个 n 大小的向量)并且您想通过输入前 16 个,然后是第二个 16,以此类推直到第 16 个来训练网络16,是的,很有可能。根据您给出的示例代码,您需要做的就是创建一个循环 2 次的 for 循环(因为在您的示例中,有 4 个数据,您希望将它们输入一组 2)并且,更改这些代码行:for step in range(500):        loss_train, _ = sess.run([loss, train], feed_dict=feeddict_train)`至for step in range(500):        temp_list = [] #an empty list        for i in range(0,4,2):               loss_train, _ = sess.run([loss, train], feed_dict={x:x_train[i:i+2], y:y_train[i:i+2]}               temp_list.append(loss_train) #append the loss of the network for each group of data.这些将允许网络独立地使用两组数据进行训练并从中学习。您可以在新的 for 循环之前简单地创建一个空列表并将其中的输出连接起来。希望这可以帮助。如果我错误地理解了您的问题,请告诉我。干杯。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python