# 导入数值计算模块
import numpy as np
import tensorflow as tf # 创建计算会话
sess = tf.Session()
# 生成数据,创建占位符和变量A
x_vales = np.random.normal(, 0.1, )
y_vals = np.repeat(., )
x_data = tf.placeholder(shape=[], dtype=tf.float32)
y_target = tf.placeholder(shape=[], dtype=tf.float32)
A = tf.Variable(tf.random_normal(shape=[])) # 增加乘法操作
my_output = tf.multiply(x_data, A)
# 增加L2正则损失函数
loss = tf.square(my_output - y_target) # 在运行之前,需要初始化变量
#init = tf.initialize_all_tables()
init = tf.tables_initializer()
sess.run(init) # 声明变量的优化器 # 学习率的选取
my_opt = tf.train.GradientDescentOptimizer(learning_rate=0.2)
train_step = my_opt.minimize(loss) # 训练算法
for i in range():
rand_index = np.random.choice()
rand_x = [x_vales[rand_index]]
rand_y = [y_vals[rand_index]]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
if (i + ) % == :
print('Step #' + str(i + ) + 'A = ' + str(sess.run(A)))
print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
#
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.

 
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:

# 导入数值计算模块
import numpy as np
import tensorflow as tf # 构建计算图
# 生成数据,创建占位符和变量A
x_vales = np.random.normal(, 0.1, )
y_vals = np.repeat(., )
x_data = tf.placeholder(shape=[], dtype=tf.float32)
y_target = tf.placeholder(shape=[], dtype=tf.float32)
A = tf.Variable(tf.random_normal(shape=[])) # 增加乘法操作
my_output = tf.multiply(x_data, A)
# 增加L2正则损失函数
loss = tf.square(my_output - y_target) # 声明变量的优化器
# 学习率的选取
my_opt = tf.train.GradientDescentOptimizer(learning_rate=0.2)
train_step = my_opt.minimize(loss) # 运行计算图 # 创建计算会话
sess = tf.Session() # 内部状态初始化完成后,您就可以像运行任何其他 tf.Tensor 一样运行 inputs:
# 特征列和层一样具有内部状态,因此通常需要将它们初始化。分类列会在内部使用对照表,而这些表需要单独的初始化指令 tf.tables_initializer。 var_init = tf.global_variables_initializer()
table_init = tf.tables_initializer() sess.run((var_init, table_init)) # 训练算法
for i in range():
rand_index = np.random.choice()
rand_x = [x_vales[rand_index]]
rand_y = [y_vals[rand_index]]
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
if (i + ) % == :
print('Step #' + str(i + ) + 'A = ' + str(sess.run(A)))
print('Loss = ' + str(sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})))
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
Step #25A = [10.501938]
Loss = [0.00203205]
Step #50A = [9.105795]
Loss = [0.2731857]
Step #75A = [9.097782]
Loss = [0.5107153]
Step #100A = [9.557248]
Loss = [0.00200771]

  

 
 
 
 

最新文章

  1. 【MySQL】drop大表
  2. canvas实现钟表
  3. 学习iOS的一些网站收藏
  4. spring配置事务
  5. IOS9中出现的错误
  6. adb getprop setprop watchprop用法
  7. Cracking the coding interview 第一章问题及解答
  8. mysql 分析5语句的优化--索引添加删除
  9. python函数说明内容格式错误
  10. ABP 找不到版本为 (>= 1.0.0-preview1-27891) 的包 Microsoft.AspNetCore.SignalR 错误
  11. Android监听屏幕解锁和判断屏幕状态
  12. 结合Mybatis源码看设计模式——外观模式
  13. Python——Pycharm创建Django项目
  14. linux driver ------ GPIO的驱动编写和调用
  15. simulation vs emulation
  16. Java Swing 中使用 EventQueue
  17. Java 笔记——在 IDEA 中使用 Maven 配置和使用 MyBatis
  18. Android-FragmentPagerAdapter刷新无效的解决方案
  19. 合并两个dt
  20. Hive学习之路 (四)Hive的连接3种连接方式

热门文章

  1. html页面比较长,如何用js实现网页一打开显示在网页的中部?
  2. 2017 计蒜之道 初赛 第一场 B阿里天池的新任务(简单)
  3. The Bells are Ringing(枚举)
  4. (转载)C++ string中find() ,rfind() 等函数 用法总结及示例
  5. Apache Ambari 2.7.3.0 离线安装
  6. swift -从相册中选择照片并上传
  7. IntelliJ IDEA配置本地Tomcat方法---亲测有效
  8. [转]MySQL5字符集支持及编码研究
  9. struct init
  10. TCP/IP学习笔记(4)------ICMP,ping,traceroute