【问题标题】:Is it possible to train the inputs of deep neural network part by part是否可以逐部分训练深度神经网络的输入
【发布时间】:2020-02-03 09:40:38
【问题描述】:

我想知道是否可以逐部分训练神经网络的输入。例如,假设我有输入 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)

plt.plot(train_loss, 'r', label='train_loss')
plt.plot(test_loss, 'b', label='test_loss')
plt.legend(loc='best')

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

我该怎么做?如果可能的话,你能帮我这样做吗?

提前谢谢你。

【问题讨论】:

    标签: python neural-network deep-learning


    【解决方案1】:

    由于您的问题不准确,因此您的问题的解释方式很少。

    第一解读: 如果您要问的是,如果您的神经网络接收到一个大小为 256 的 输入 向量并且 输出 一个大小为 256 的向量,那么答案是否定的,您可以t 输入向量的一部分作为输入并期望它工作。

    二次解读: 如果您要问的是,如果您有 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 循环 之前创建一个空列表并将其中的输出连接起来。

    希望这会有所帮助。如果我错误地理解了您的问题,请告诉我。干杯。

    【讨论】:

    • 非常感谢。我得到了它。只是最后一个问题,我检查了这篇文章,sci-hub.tw/10.1109/lwc.2017.2757490 在第二页的最后三行和第三页的开头,.. 它描述了我问的同样的想法,他从 256 个神经元输入中提取了 16 个输入,.. ..这是否意味着它类似于您的第一个解释或类似于我的问题? ...再次感谢
    • 不客气 :) 我浏览了这篇文章,我的想法是,它们将 16 x 16 位向量分组以形成一个长 256 位输入到神经网络。可能是最终有 16 个输出,每组 1 个预测的原因。这与我的任何一种解释都无关。
    • 好的,开始吧。我会努力解决它并了解所有细节。
    • 好吧,你的占位符接受一个形状为 (?, 64) 的张量,但你给它提供了一个形状为 (64,1024) 的张量。您必须更改占位符的形状或输入张量的形状。
    • 很高兴听到这个消息 :) 不幸的是,我无法在这件事上为您提供帮助,因为我根本没有使用过 Matlab。
    猜你喜欢
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2020-11-24
    • 2020-05-11
    相关资源
    最近更新 更多