1. # 导入数值计算模块
  2. import numpy as np
  3. import tensorflow as tf
  4.  
  5. # 创建计算会话
  6. sess = tf.Session()
  7. # 生成数据,创建占位符和变量A
  8. x_vales = np.random.normal(, 0.1, )
  9. y_vals = np.repeat(., )
  10. x_data = tf.placeholder(shape=[], dtype=tf.float32)
  11. y_target = tf.placeholder(shape=[], dtype=tf.float32)
  12. A = tf.Variable(tf.random_normal(shape=[]))
  13.  
  14. # 增加乘法操作
  15. my_output = tf.multiply(x_data, A)
  16. # 增加L2正则损失函数
  17. loss = tf.square(my_output - y_target)
  18.  
  19. # 在运行之前,需要初始化变量
  20. #init = tf.initialize_all_tables()
  21. init = tf.tables_initializer()
  22. sess.run(init)
  23.  
  24. # 声明变量的优化器
  25.  
  26. # 学习率的选取
  27. my_opt = tf.train.GradientDescentOptimizer(learning_rate=0.2)
  28. train_step = my_opt.minimize(loss)
  29.  
  30. # 训练算法
  31. for i in range():
  32. rand_index = np.random.choice()
  33. rand_x = [x_vales[rand_index]]
  34. rand_y = [y_vals[rand_index]]
  35. sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
  36. if (i + ) % == :
  37. print('Step #' + str(i + ) + 'A = ' + str(sess.run(A)))
  38. print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
  39. #
  40. d =

An Op that initializes all tables. Note that if there are not tables the returned Op is a NoOp.

 
 
特征列和层一样具有内部状态,因此通常需要将它们初始化
 

Feature columns can have internal state, like layers, so they often need to be initialized. Categorical columns use lookup tables internally and these require a separate initialization op, tf.tables_initializer.

 
  1. var_init = tf.global_variables_initializer()
    table_init = tf.tables_initializer()
    sess = tf.Session()
    sess.run((var_init, table_init))

Once the internal state has been initialized you can run inputs like any other tf.Tensor:

  1. # 导入数值计算模块
  2. import numpy as np
  3. import tensorflow as tf
  4.  
  5. # 构建计算图
  6. # 生成数据,创建占位符和变量A
  7. x_vales = np.random.normal(, 0.1, )
  8. y_vals = np.repeat(., )
  9. x_data = tf.placeholder(shape=[], dtype=tf.float32)
  10. y_target = tf.placeholder(shape=[], dtype=tf.float32)
  11. A = tf.Variable(tf.random_normal(shape=[]))
  12.  
  13. # 增加乘法操作
  14. my_output = tf.multiply(x_data, A)
  15. # 增加L2正则损失函数
  16. loss = tf.square(my_output - y_target)
  17.  
  18. # 声明变量的优化器
  19. # 学习率的选取
  20. my_opt = tf.train.GradientDescentOptimizer(learning_rate=0.2)
  21. train_step = my_opt.minimize(loss)
  22.  
  23. # 运行计算图
  24.  
  25. # 创建计算会话
  26. sess = tf.Session()
  27.  
  28. # 内部状态初始化完成后,您就可以像运行任何其他 tf.Tensor 一样运行 inputs:
  29. # 特征列和层一样具有内部状态,因此通常需要将它们初始化。分类列会在内部使用对照表,而这些表需要单独的初始化指令 tf.tables_initializer。
  30.  
  31. var_init = tf.global_variables_initializer()
  32. table_init = tf.tables_initializer()
  33.  
  34. sess.run((var_init, table_init))
  35.  
  36. # 训练算法
  37. for i in range():
  38. rand_index = np.random.choice()
  39. rand_x = [x_vales[rand_index]]
  40. rand_y = [y_vals[rand_index]]
  41. sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
  42. if (i + ) % == :
  43. print('Step #' + str(i + ) + 'A = ' + str(sess.run(A)))
  44. print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
  1. 2018-05-12 16:57:22.358693: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
  2. Step #25A = [10.501938]
  3. Loss = [0.00203205]
  4. Step #50A = [9.105795]
  5. Loss = [0.2731857]
  6. Step #75A = [9.097782]
  7. Loss = [0.5107153]
  8. Step #100A = [9.557248]
  9. Loss = [0.00200771]

  

 
 
 
 

train_action的更多相关文章

随机推荐

  1. No-1.第一个 Python 程序

    1. 第一个 HelloWorld 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程序的 ...

  2. vlmcsd-1111-2017-06-17

    Source and binaries: http://rgho.st/6c6R7RwMZ   全部编译好了 https://www.upload.ee/files/7131474/vlmcsd-11 ...

  3. 在计算机中简单的hello程序的运行

    我之前很好奇就是你所写的代码到底是如何在计算机中执行的,到底是怎样开始,从哪里开始,在哪里执行,到哪里结束,之间到底是怎么的一个过程,带着这些问题,逐渐看这方面的书籍,最近买的<深入理解计算机系 ...

  4. 诊断:CLSRSC-400: A system reboot is required to continue installing.

    Linux7.5安装Grid Infrastructure 12.2.0.1时,在root.sh时会报错 2018/01/30 09:19:28 CLSRSC-330: Adding Clusterw ...

  5. getDate() 获取时间 如2018年09月21日 11:32:11

    function p(s) { return s < 10 ? '0' + s: s;} function getDate() { var myDate = new Date(); //获取当前 ...

  6. 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西

    知识点 os,os.path模块中关于文件.目录常用的函数使用方法 在使用os模块,需要先进行import操作: import os os模块中关于文件/目录常用的函数使用方法 函数名 函数作用 示例 ...

  7. chrome最强大的浏览器插件推荐,只要你会用其他的插件你可以删除了

    我们在学习和工作中经常会需要用到各种各样不同需求的插件,结果chrome插件越装越多,chrome浏览器也越来越慢!有时候链我们自己都懵圈了,一时间都想不起来这个插件是干什么用的.更可气的是,很多时候 ...

  8. Matlab学习笔记(三)

    二.MATLAB基础知识 (四)数组 MATLAB总是把数组看作存储和运算的基本单位,标量数据也被看作是(1×1)的数组 一维数组的创建 创建一维数组的几种方法:(e_two_14.m) 直接输入法: ...

  9. 【BZOJ 2118】 墨墨的等式(Dijkstra)

    BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...

  10. Android SwipeSelector

     Android SwipeSelector Android SwipeSelector是github上一个第三方开源的项目,其项目主页:https://github.com/roughike/S ...