# 导入数值计算模块
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. python 深复制和浅复制

    https://www.python-course.eu/python3_deep_copy.php-------------------------------------------------- ...

  2. [Python3网络爬虫开发实战] 1.5.4-RedisDump的安装

    RedisDump是一个用于Redis数据导入/导出的工具,是基于Ruby实现的,所以要安装RedisDump,需要先安装Ruby. 1. 相关链接 GitHub:https://github.com ...

  3. web前端开发——css

    一.css介绍 1.css是什么? Cascading Style Sheets缩写,层叠样式表.样式定义如何显示HTML元素,样式通常又会存在于样式表中. 2.为什么需要css? 使HTML页面变得 ...

  4. Codeforces915G. Coprime Arrays

    n<=2e6的数组,m<=2e6个询问,对1<=i<=m的每个i问:只用<=i的数字填进数组,有多少种方案使数组的总gcd=1.强制把每个询问的答案求出来. 比如说现在有 ...

  5. 【Intellij】Intellij Idea 2017创建web项目及tomcat部署实战

    相关软件:Intellij Idea2017.jdk16.tomcat7 Intellij Idea直接安装(可根据需要选择自己设置的安装目录),jdk使用1.6/1.7/1.8都可以,主要是配置好系 ...

  6. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  7. DELPHI IDFTP

    FTP是一个标准协议,它是在计算机和网络之间交换文件的最简单的方法. FTP也是应用TCP/IP协议的应用协议标准.FTP通常于将作者的文件上传至服务器,或从服务器上下传文件的一种普遍的使用方式作为用 ...

  8. SQL SERVER 自增字段相关问题

    SET IDENTITY_INSERT Data0048_TEST ON --给自增列赋值 DBCC CHECKIDENT(TableName) --查看某个表中的自增列当前的值 DBCC CHECK ...

  9. [bzoj1978][BeiJing2010]取数游戏 game_动态规划_质因数分解

    取数游戏 game bzoj-1978 BeiJing-2010 题目大意:给定一个$n$个数的$a$序列,要求取出$k$个数.假设目前取出的数是$a_j$,那么下次取出的$a_k$必须保证:$j&l ...

  10. 激活IDEA 2019.1

    First step: 先下载jar包JetbrainsCrack.jar,把它放到你认为合适的文件夹内, 我放在了安装目录C:\Program Files\JetBrains\IntelliJ ID ...