[英]Tensorflow retrain neural network with different data
I have a list of inputs
to the neural network for example 例如,我有神经网络的inputs
列表
list_of_inputs = [inputs1, inputs2, inputs3, ... ,inputsN]
*and also a corresponding list of labels * *以及相应的标签列表*
list_of_labels = [label1, label2, label3, ..., labelN]
I want to feed/train each pair of input,label
into the neural network, record the loss and then train the next pair of input,label
on the same network and record the loss, etc. for all the input,label
pairs. 我想将每对input,label
/训练到神经网络中,记录损失,然后在同一网络上训练下一对input,label
,并记录所有input,label
对的损失等。
Note: I don't want to reinitialize the weights every time a new input,label
is added, I want to use the trained weights from the previous pair. 注意:我不想每次添加新的input,label
都重新初始化权重,我想使用前一对中训练后的权重。 The network is shown below ( where you can see I am also printing the loss). 网络如下所示(您可以在此处看到我也在打印损失)。 How would I go about this? 我将如何处理?
with tf.name_scope("nn"):
model = tf.keras.Sequential([
tfp.layers.DenseFlipout(64, activation=tf.nn.relu),
tfp.layers.DenseFlipout(64, activation=tf.nn.softmax),
tfp.layers.DenseFlipout(np.squeeze(labels).shape[0])
])
logits = model(inputs)
loss = tf.reduce_mean(tf.square(labels - logits))
train_op_bnn = tf.train.AdamOptimizer().minimize(loss)
init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init_op)
for i in range(100):
sess.run(train_op_bnn)
print(sess.run(loss))
EDIT: 编辑:
The issue is that when I try to format the network in a function as below: 问题是,当我尝试使用以下功能格式化网络时:
init_op = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init_op)
inputs,labels = MEMORY[0]
logits, model_losses = build_graph(inputs)
loss = tf.reduce_mean(tf.square(labels - logits))
train_op_bnn = tf.train.AdamOptimizer().minimize(loss)
sess.run(train_op_bnn)
print(sess.run(loss))
I get an error: 我收到一个错误:
FailedPreconditionError Traceback (most recent call last)
<ipython-input-95-5ca77fa0606a> in <module>()
36 train_op_bnn = tf.train.AdamOptimizer().minimize(loss)
37
---> 38 sess.run(train_op_bnn)
39 print(sess.run(loss))
40
logits, model_losses = build_graph(inputs)
loss = tf.reduce_mean(tf.square(labels - logits))
train_op_bnn = tf.train.AdamOptimizer().minimize(loss)
should be above 应该在上面
with tf.Session() as sess:
and above your init_op
definition 在您的init_op
定义之上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.