Tensorflow:在单次运行中分配多个变量值,无需重新计算其他表达式

2024-01-04

我是 Tensorflow 的新手,很抱歉,因为这似乎是一个非常基本的问题,但不幸的是我在 Google 上找不到任何内容,也许我使用了错误的关键字。

我有一些从占位符派生的表达式(据我了解张量流的逻辑),以及一些需要在不重新计算“占位符”表达式的情况下进行计算的变量。下面是我相当丑陋的代码(应该是手动构建的 3 层神经网络),其中评估发生在循环中。

问题是,当我计算派生表达式(ys、delta)时,我想在一次运行中评估所有权重,而不会错误地重新计算 ys 和 delta,我认为目前应该会发生这种情况。该代码中可能存在其他错误,导致其无法正常工作(但是,以这种方式编写的 1 层代码可以正常工作,并且达到预期的 92% 的准确率),但至少在计算阶段完成之前很难弄清楚。没有搞砸。

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


#launch tensorflow session
import tensorflow as tf
sess = tf.InteractiveSession(config=tf.ConfigProto(
  intra_op_parallelism_threads=4)) 

def nonlin(x,deriv=False): # I want my custom activation function
    if(deriv==True):
        return tf.nn.sigmoid(x)*(1 - tf.nn.sigmoid(x))

    return tf.nn.sigmoid(x)



#placeholders
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

# Variables ----------------

#weights and biases
W1 = tf.Variable(tf.zeros([784,10])) # 784x10 matrix (because we have 784 input features and 10 outputs)
b1 = tf.Variable(tf.zeros([10]))

W2 = tf.Variable(tf.zeros([10,10])) 
b2 = tf.Variable(tf.zeros([10]))

W3 = tf.Variable(tf.zeros([10,10])) 
b3 = tf.Variable(tf.zeros([10]))

# ---------------------

sess.run(tf.global_variables_initializer())


# derived expressions -------------------------
# Forward pass 

y1 = nonlin(tf.matmul(x,W1) + b1)
y2 = nonlin(tf.matmul(y1,W2) + b2)
y3 = nonlin(tf.matmul(y2,W3) + b3)


error3 = y_ - y3 # quadratic cost derivative



# Backward pass 
delta3 = tf.multiply(error3,nonlin(y3, deriv=True))  #assign delta

error2 = tf.matmul(delta3,W3, transpose_b=True)
delta2 = tf.multiply(error2,nonlin(y2, deriv=True))

error1 = tf.matmul(delta2,W2, transpose_b=True)
delta1 = tf.multiply(error1,nonlin(y1, deriv=True))


learning_rate = 0.1

# And my ugly update step which is not working:------------------------

w1_assign = tf.assign(W1, tf.add(W1, tf.multiply(learning_rate, tf.reduce_mean(tf.matmul(tf.expand_dims(x,-1), tf.expand_dims(delta1,-1), transpose_b=True), 0)) ))
b1_assign = tf.assign(b1, tf.add(b1, tf.multiply(learning_rate, tf.reduce_mean(delta1, 0)) ))

w2_assign = tf.assign(W2, tf.add(W2, tf.multiply(learning_rate, tf.reduce_mean(tf.matmul(tf.expand_dims(y1,-1), tf.expand_dims(delta2,-1), transpose_b=True), 0)) ))
b2_assign = tf.assign(b2, tf.add(b2, tf.multiply(learning_rate, tf.reduce_mean(delta2, 0)) ))

w3_assign = tf.assign(W3, tf.add(W3, tf.multiply(learning_rate, tf.reduce_mean(tf.matmul(tf.expand_dims(y2,-1), tf.expand_dims(delta3,-1), transpose_b=True), 0)) ))
b3_assign = tf.assign(b3, tf.add(b3, tf.multiply(learning_rate, tf.reduce_mean(delta3, 0)) ))

# accuracy evaluation ----------------------

correct_prediction = tf.equal(tf.argmax(y3,1), tf.argmax(y_,1)) #a list of booleans.
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


# Main loop:----------------------

for epoch in range(1000):
  batch = mnist.train.next_batch(1000)
  # apply across batch

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

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

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


  # precision computation

  print(str(accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})) + " / epoch: " + str(epoch))  # evaluate

UPDATE:

基于这个答案 https://stackoverflow.com/a/41309537/1692060,看起来如果我在列表中向 sess.run 提供参数,我将只初始化所有中间变量一次,但顺序未知。然后我尝试了我的网络,进行了以下修改,其中包括在列表中传递参数和附加变量来存储新的权重,并使它们不会与原始的权重混淆(很抱歉代码很长,但我试图让它立即为您执行) :

def nonlin(x,deriv=False):
    if(deriv==True):
        return tf.nn.sigmoid(x)*(1 - tf.nn.sigmoid(x))

    return tf.nn.sigmoid(x)


#We start building the computation graph by creating nodes for the input images and target output classes.
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])




#weights and biases
W1 = tf.Variable(tf.random_uniform([784,400])) # 784x10 matrix (because we have 784 input features and 10 outputs)
b1 = tf.Variable(tf.random_uniform([400]))

W2 = tf.Variable(tf.random_uniform([400,30])) # 784x10 matrix (because we have 784 input features and 10 outputs)
b2 = tf.Variable(tf.random_uniform([30]))

W3 = tf.Variable(tf.random_uniform([30,10])) # 784x10 matrix (because we have 784 input features and 10 outputs)
b3 = tf.Variable(tf.random_uniform([10]))


# temporary containers to avoid messing up computations
W1tmp = tf.Variable(tf.zeros([784,400])) # 784x10 matrix (because we have 784 input features and 10 outputs)
b1tmp = tf.Variable(tf.zeros([400]))

W2tmp = tf.Variable(tf.zeros([400,30])) # 400x30 matrix as second layer
b2tmp = tf.Variable(tf.zeros([30]))

W3tmp = tf.Variable(tf.zeros([30,10])) # 30x10 matrix (because we have 10 outputs)
b3tmp = tf.Variable(tf.zeros([10]))




#Before Variables can be used within a session, they must be initialized using that session. 
sess.run(tf.global_variables_initializer())


# multiplication across batch
# The tf.batch_matmul() op was removed in 3a88ec0. You can now use tf.matmul() to perform batch matrix multiplications (i.e. for tensors with rank > 2).

# Forward pass 

y1 = nonlin(tf.matmul(x,W1) + b1)
y2 = nonlin(tf.matmul(y1,W2) + b2)
y3 = nonlin(tf.matmul(y2,W3) + b3)


error3 = y_ - y3 # quadratic cost derivative



# Backward pass 
# error and y have same dimensions. It's only W that is unique
delta3 = tf.multiply(error3,nonlin(y3, deriv=True))  #assign delta



error2 = tf.matmul(delta3,W3, transpose_b=True)
delta2 = tf.multiply(error2,nonlin(y2, deriv=True))

error1 = tf.matmul(delta2,W2, transpose_b=True)
delta1 = tf.multiply(error1,nonlin(y1, deriv=True))



learning_rate = tf.constant(3.0)

# we first assign the deepest level to avoid extra evaluations

#with tf.control_dependencies([y1,y2,y3,delta1,delta2,delta3]): 
w1_assign = tf.assign(W1tmp, tf.add(W1, tf.multiply(learning_rate, tf.reduce_mean(tf.matmul(tf.expand_dims(x,-1), tf.expand_dims(delta1,-1), transpose_b=True), 0)) ))
b1_assign = tf.assign(b1tmp, tf.add(b1, tf.multiply(learning_rate, tf.reduce_mean(delta1, 0)) ))

w2_assign = tf.assign(W2tmp, tf.add(W2, tf.multiply(learning_rate, tf.reduce_mean(tf.matmul(tf.expand_dims(y1,-1), tf.expand_dims(delta2,-1), transpose_b=True), 0)) ))
b2_assign = tf.assign(b2tmp, tf.add(b2, tf.multiply(learning_rate, tf.reduce_mean(delta2, 0)) ))

w3_assign = tf.assign(W3tmp, tf.add(W3, tf.multiply(learning_rate, tf.reduce_mean(tf.matmul(tf.expand_dims(y2,-1), tf.expand_dims(delta3,-1), transpose_b=True), 0)) ))
b3_assign = tf.assign(b3tmp, tf.add(b3, tf.multiply(learning_rate, tf.reduce_mean(delta3, 0)) ))


w1_ok = tf.assign(W1,W1tmp)
w2_ok = tf.assign(W2,W2tmp)
w3_ok = tf.assign(W3,W3tmp)

b1_ok = tf.assign(b1,b1tmp)
b2_ok = tf.assign(b2,b2tmp)
b3_ok = tf.assign(b3,b3tmp)



#accuracy evaluation

correct_prediction = tf.equal(tf.argmax(y3,1), tf.argmax(y_,1)) #a list of booleans.
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))



# we can use only single batch, just to check that everything works
#batch = mnist.train.next_batch(1000)

for epoch in range(10000):
  batch = mnist.train.next_batch(1000)
  #train_step.run(feed_dict={x: batch[0], y_: batch[1]})


  #When you call sess.run([x, y, z]) once, TensorFlow executes each op that those tensors depend on one time only (unless there's a tf.while_loop() in your graph). If a tensor appears twice in the list (like mul in your example), TensorFlow will execute it once and return two copies of the result. To run the assignment more than once, you must either call sess.run() multiple times, or use tf.while_loop() to put a loop in your graph.

  # write new variable values to containers
  sess.run([w1_assign,w2_assign,w3_assign,b1_assign,b2_assign,b3_assign] , feed_dict={x: batch[0], y_: batch[1]})

  # write container contents into variables in a separate session
  sess.run([w1_ok,w2_ok,w3_ok,b1_ok,b2_ok,b3_ok])# , feed_dict={x: batch[0], y_: batch[1]})

  # precision computation

  print(str(accuracy.eval(feed_dict={x: batch[0], y_: batch[1]})) + " / epoch: " + str(epoch))  # evaluate

所以问题是它是否至少是正确的 Tensorflow 代码?我发现网络结构和学习率给出了一些结果,但它们似乎仍然很差(大约 75%)。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow:在单次运行中分配多个变量值,无需重新计算其他表达式 的相关文章

随机推荐

  • ZendDebugger 无法在 Mint 12 中打开 libssl.so.0.9.8

    我安装了 apache 和 php 现在使用 ZendDebugger 我并修改了 php ini 的描述方式 当我启动 apache 时 我在日志中收到以下错误消息 Failed loading usr lib php5 zend Zen
  • 优化重片段着色器的性能

    我需要帮助优化以下一组着色器 Vertex precision mediump float uniform vec2 rubyTextureSize attribute vec4 vPosition attribute vec2 a Tex
  • 在复制构造函数中调用赋值运算符

    这种复制构造函数的实现有一些缺点吗 Foo Foo const Foo i foo this i foo 我记得 在一些书中建议从赋值运算符调用复制构造函数并使用众所周知的交换技巧 但我不记得为什么 是的 这是一个坏主意 所有用户定义类型的
  • 如何使用搜索命令搜索点字符?

    我正在尝试使用 Vim 中的搜索命令 Rs F T X R range F text to find T text to replace with X options 但是 当我想搜索 时 点字符 我遇到一些问题 任务 替换所有出现的 空格
  • Perl 诅咒::UI

    我正在尝试使用 Curses UI 库http search cpan org dist Curses UI http search cpan org dist Curses UI 在 Linux karmic 上构建 UI 我可以创建一个
  • 检测 Windows Kit 8.0 和 Windows Kit 8.1 SDK

    我正在为 Windows 平板电脑 Windows Phone 和 Windows 应用商店应用程序编写测试脚本 这些脚本主要适用于 Visual Studio 2012 和 Windows Kit 8 0 SDK Microsoft 似乎
  • Jupyter 笔记本:本地存储的 pdf 文档的超链接在 Chrome 中停止工作

    我有大量的 Jupyter Notebook 其中许多都有指向本地存储的 pdf 文档的超链接 不久前 这些链接在我的 iMac 上的 Chrome 中停止工作 单击链接时 会打开一个带有正确地址的新选项卡 但页面只是黑色 当我在 MacB
  • Squeak/Pharo Web 服务的微框架

    许多语言都有用于编写非常小的网站或 Web 服务的微框架 例如用于 Python 的 Flask 或用于 Ruby 的 Sinatra 在 Squeak 上 似乎没有任何类似的东西 伊利亚特 海边 和 AIDA 都非常重 只是提供了一点服务
  • 如何在vb.net中使用打开文件对话框指定路径?

    在我的应用程序的第一次启动中 我需要指定一个路径来保存一些文件 但在打开文件对话框中 我似乎必须选择要打开的文件 如何只指定文件夹而不打开文件 比如 C config 这是我的代码 If apppath Then Dim fd As Ope
  • 如何使用 SQL 查询更新表中的多行?

    I am new to SQL and C 如屏幕截图所示 我想通过仅插入数量 描述和价格值来更新 订单详细信息 表中的 3 行 订单 3 DataGridView2 我使用 Order Number 和 DateTime 的组合来使订单详
  • Promise.all 与 Firebase DataSnapshot.forEach

    我有几个 HTML 选择 下拉菜单 它们是从名为 states 的 Firebase 节点填充的 见下图 选择城市后 将触发以下函数并检索该城市中发生的所有会议 有一个单独的 会议 节点 每个会议都有各种键 值对 例如街道 时间等 我 认为
  • 使用 jquery 添加新 css 规则的最佳方法?

    我在网页上动态插入一些 html 在检测到用户的事件后 这个 html 需要一些 css 样式 我想知道使用 jQuery 最简洁的方法是什么 我不是网站开发人员 所以我无法将这些规则添加到原始CSS中 假设我已经插入了一些没有样式的 ht
  • django-pagination 可以每页进行多个分页吗?

    如果不能 那么是否有任何其他替代方案 Django 的本机分页或备用包 允许每页多个分页 我想显示大约 5 个对象的列表 每个对象都有自己的分页上下文 为了方便起见 这里是django 分页文档 http pypi python org p
  • 使用消费计划的 Azure 功能的 VNET 集成

    我的 azure 函数正在消耗计划上运行 它需要访问在 Azure VNET 上的 VM 上运行的资源 该资源无法通过 http 公开 除了切换到应用服务计划之外还有其他解决方案吗 目前来看 这是不可能的 VNet 集成功能需要标准 高级或
  • Windows 窗体的每像素碰撞检测算法

    我正在寻找每像素碰撞检测算法 方法Windows 窗体 我已经搜索过了 但只找到了一个 XNA 如下所示 这样的算法不是很符合Windows Forms的概念吗
  • 客户端IP地址的最大长度[重复]

    这个问题在这里已经有答案了 可能的重复 IPv6 地址的文本表示的最大长度 https stackoverflow com questions 166132 maximum length of the textual representat
  • 如何在 UPDATE 语句中使用用户定义的变量?

    我试图回答另一个所以问题 https stackoverflow com questions 18404726并突然面临以下问题 分数应分配给得分最高的 3 个 mrk 组 grp 每个班级 sec 得分最高的组得5分 排名第二的组得3分
  • Hibernate Embedded/Embeddable 不为空异常

    在拥有类中 Embedded private LatLon location 在引用的类中 Embeddable public class LatLon implements Serializable private double lat
  • 使用子查询更新

    我的查询有问题 我有一张巨大的桌子 上面有来自德国的邮政编码 名为 Postleitzahlen 还有另一张名为 Firmen 的公司表 结构是这样的 Firmen ID City State ZipCode Postleitzahlen
  • Tensorflow:在单次运行中分配多个变量值,无需重新计算其他表达式

    我是 Tensorflow 的新手 很抱歉 因为这似乎是一个非常基本的问题 但不幸的是我在 Google 上找不到任何内容 也许我使用了错误的关键字 我有一些从占位符派生的表达式 据我了解张量流的逻辑 以及一些需要在不重新计算 占位符 表达