# 导入数值计算模块
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. android-async-http框架库使用基础

    开源项目链接 android-async-http仓库:git clone https://github.com/loopj/android-async-http android-async-http ...

  2. 常量指针(pointer to constant)和指针常量(constant pointer)

    一个指针可以操作两个实体,一个是指针值(即地址),一个是间访值(即指向的实体).于是指针的常量性也分为两种:常量指针(pointer to constant)和指针常量(constant pointe ...

  3. glibc库函数,系统调用API

    glibc封装了大部分系统API,我们一般都是使用glibc封装的接口进行系统调用,碰到一些没有封装的接口,可以通过这个 函数syscall 进行系统调用.   /* Invoke `system c ...

  4. PS学习一

    1.使用缩放工具时是对文档窗口进行的缩放,它只影响视图比例:而对图像的缩放则是指对图像文件本身进行的物理缩放,它会使图像的内容变大或变小. 2.分辨率是指单位长度内包含的像素点的数量,它的单位通常为像 ...

  5. PHP:Mysql 基础类

    文章来源:http://www.cnblogs.com/hello-tl/p/7592547.html <?php /** * __construct($Mysql_config) 构造函数 $ ...

  6. NOI模拟赛(3.8)Problem B

    Description Alice和Bob在玩一个游戏,给出一张n*m的棋盘,上面有一些点是障碍,游戏的开始,Alice选定棋盘上任意一个不是障碍的格子,并且将一枚棋子放在其中,然后Bob先手,两人轮 ...

  7. 在jupyter notebook 中编辑公式

    jupyter notebook是一个python的交互式开发环境,广泛应用于数据分析的场景下. 在jupyter notebook中,还可以很方便的编辑数学公式. 1.Markdown状态 编辑公式 ...

  8. xcap发包工具的简单使用3(报文描述)

    之前详细介绍了如何构造以及发送报文,现在简单对报文描述一下 1.Ethernet MAC:填写报文目的mac和源mac地址 Vlan:支持单vlan,QinQ,如果有更多的vlan,请填写在“More ...

  9. [K/3Cloud] 创建一个操作校验器

    概念: 定义了一个校验器对象,通常是添加到业务单据操作服务插件,用于对操作的合法性进行校验.继承自校验器抽象对象AbstractValidator. 示例: 新建一个类,继承自AbstractVali ...

  10. 【ZJOI2017 Round1练习】D8T1 mushroom(点分治)

    题意: 思路: num[a[u]]表示存在a[u]这个颜色且终点在u子树中的链长总和 ans[i]表示以当前的u为根,前面的子树对i的贡献之和 ..]of longint; size,f,ans,su ...