# 导入数值计算模块
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]

  

 
 
 
 

train_action的更多相关文章

随机推荐

  1. jquery 应用

    http://www.jq22.com/ gwj6396668@163.com

  2. SVN文件库移植(转)

     SVN文件库移植(转) 分类: 项目管理2013-04-19 11:06 161人阅读 评论(0) 收藏 举报 公司以前用的SVN是安装在windows2003下,用了一年多,现在大家觉得很慢,强烈 ...

  3. IIS启用32位应用程序兼容

    针对服务器出现html和jsp页面都可以应用,但唯独asp页面打不开的一种情况 win7的IIS运行在32状态下,原因是ASP程序必须在32位下才能使用ACCESS 设置办法: 打开IIS管理器,点应 ...

  4. python 2 encode and decode

    https://docs.python.org/2/howto/unicode.html a Unicode string is a sequence of code points, which ar ...

  5. torch.nn.Linear()函数的理解

    import torch x = torch.randn(128, 20) # 输入的维度是(128,20)m = torch.nn.Linear(20, 30) # 20,30是指维度output ...

  6. 启用Windows10的Linux子系统并安装图形界面

    前言 目前市面上的PC电脑主要运行着四大类系统,它们分别是微软的Windows.苹果的MacOS.Linux的发行版以及Unix类系统.其中Linux和Unix都是开源的,因此市面出现的众多基于Lin ...

  7. PHP实现QQ第三方登录的方法

    前言: PHP实现QQ快速登录,罗列了三种方法 方法一:面向过程,回调地址和首次触发登录写到了一个方法页面[因为有了if做判断], 方法二,三:面向对象 1.先调用登录方法,向腾讯发送请求,2.腾讯携 ...

  8. PHP:GD库 图片水印处理

    文章来源:http://www.cnblogs.com/hello-tl/p/7592974.html <?php /** * 处理图片类 * 1.添加文字水印 * 2.添加图片水印 * 3.压 ...

  9. random,json,pickle,hashlib,shutil,hmac,shelve 模块

    一,复习 ''' 项目开发规范 ATM -- bin: 可执行文件 # run.py import os import sys BASE_DIR = os.path.dirname(os.path.d ...

  10. UVA 230 Borrowers (STL 行读入的处理 重载小于号)

    题意: 输入若干书籍和作者名字,然后先按作者名字升序排列,再按标题升序排列,然后会有3种指令,BORROW,RETURN, SHELVE. BORROW 和 RETURN 都会带有一个书名在后面,如: ...