在TensorFlow中动态更改权重 [英] Dynamically changing weights in TensorFlow

查看:1030
本文介绍了在TensorFlow中动态更改权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TensorFlow中,我正在尝试在训练期间更改权重,但结果没有变化.我试图破坏权重(设置为零),但是它似乎什么也没做(除了需要更长的时间才能完成).我想念什么?有没有一种方法可以在会话期间像常规矩阵/张量那样操纵W?

In TensorFlow, I'm trying to change weights during training, but get no change in the results. I've tried to disrupt the weights (set to zero), but it seems to do nothing (other than take longer to complete). What am I missing? Is there a way to manipulate W like a regular matrix/tensor during session?

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]), trainable=True)
W2 = tf.Variable(tf.zeros([784,10]), trainable=False)
b = tf.Variable(tf.zeros([10]))

sess.run(tf.initialize_all_variables())

y = tf.nn.softmax(tf.matmul(x,W) + b)
loss = tf.reduce_mean(tf.square(y_ - y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

for i in range(1000):
#try to change W during training
  W = W2

  W = tf.Variable(tf.zeros([784,10]))

  W.assign(tf.Variable(tf.zeros([784,10])))

  batch = mnist.train.next_batch(1)

  train_step.run(feed_dict={x: batch[0], y_: batch[1]})

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

准确性保持不变(0.82).

Accuracy remains the same (0.82).

推荐答案

使用tf.assign()时,需要为此操作指定名称:

When you use tf.assign(), you need to give a name for this operation:

W = W.assign(tf.Variable(tf.zeros([784,10])))

W= W.assign(tf.Variable(tf.zeros([784,10])))

然后再次使用W时,将执行分配操作.

Then when you use W again, the assign operation will be executed.

这篇关于在TensorFlow中动态更改权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆