train_action
# 导入数值计算模块
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的更多相关文章
随机推荐
- Redux的中间件Middleware不难,我信了^_^
Redux的action和reducer已经足够复杂了,现在还需要理解Redux的中间件.为什么Redux的存在有何意义?为什么Redux的中间件有这么多层的函数返回?Redux的中间件究竟是如何工作 ...
- Bullet:MySQL增强半同步参数rpl_semi_sync_master_wait_point值AFTER_SYNC和AFTER_COMMIT的对比实验
MySQL 5.7.22启用增强半同步复制 MySQL对该参数值的描述 Semisync can wait for slave ACKs at one of two points, AFTER_SYN ...
- 「 Luogu P3137 」X 「 USACO16FEB 」 圆形谷仓
# 题目大意 管理大大给修下 $\text{Markdown}$ 吧,严重影响做题体验啊. 这道题的意思很简单就是给你一个长度是 $n$ 的环,这个环上不均匀的分布着 $n$ 头奶牛.一头奶牛移动要花 ...
- 全国高校绿色计算大赛 预赛第二阶段(Python)
第1关统计分数的麻烦 class Task: def get_lost_scores(self, scores): s = "" index = [1 for i in range ...
- nginx虚拟主机配置实践
1.配置基于域名的虚拟主机 [root@web01 html]# egrep -v "#|^$" /application/nginx/conf/nginx.conf.defaul ...
- Android Studio配置Esri ArcGIS
1.Android Studio中新建项目: 2.打开project根目录下的build.gradle文件 repositories { jcenter() // Add the following ...
- PHP 处理接口保证数据安全性
原地址:http://blog.csdn.net/lhbeggar/article/details/46377653 php做APP接口时,如何保证接口的安全性? 1.当用户登录APP时,使用http ...
- 开发调式时生成dump文件
开发调式时,对程序生成dump文件:1:需要生成的时机,加Thread.sleep(600*1000).2:打开jvisualvm找到该程序进程号.3:jmap.
- C#上位机开发(三)—— 构建SerialAssistant雏形
上一篇简单介绍了C#的一些基本知识,并成功的Hello,World,那么从这篇开始,我们来自己动手写一个串口助手: 1.构思功能 串口助手在单片机开发中经常被用来调试,最基本的功能就是接收功能和发送功 ...
- 【BZOJ2440】完全平方数(莫比乌斯函数,容斥原理)
题意:求第k个无平方因子数 k<=10^9 思路: 感觉这东西和欧拉筛差不多……活到老学到老,退役前学点新知识也是好的 为什么二分答案的上界是2*n?连LYY都证不出来 话说约大爷一年之前就已经 ...