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的更多相关文章
随机推荐
- No-1.第一个 Python 程序
1. 第一个 HelloWorld 程序 1.1 Python 源程序的基本概念 Python 源程序就是一个特殊格式的文本文件,可以使用任意文本编辑软件做 Python 的开发 Python 程序的 ...
- vlmcsd-1111-2017-06-17
Source and binaries: http://rgho.st/6c6R7RwMZ 全部编译好了 https://www.upload.ee/files/7131474/vlmcsd-11 ...
- 在计算机中简单的hello程序的运行
我之前很好奇就是你所写的代码到底是如何在计算机中执行的,到底是怎样开始,从哪里开始,在哪里执行,到哪里结束,之间到底是怎么的一个过程,带着这些问题,逐渐看这方面的书籍,最近买的<深入理解计算机系 ...
- 诊断:CLSRSC-400: A system reboot is required to continue installing.
Linux7.5安装Grid Infrastructure 12.2.0.1时,在root.sh时会报错 2018/01/30 09:19:28 CLSRSC-330: Adding Clusterw ...
- getDate() 获取时间 如2018年09月21日 11:32:11
function p(s) { return s < 10 ? '0' + s: s;} function getDate() { var myDate = new Date(); //获取当前 ...
- 零基础入门学习Python(30)--文件系统:介绍一个高大上的东西
知识点 os,os.path模块中关于文件.目录常用的函数使用方法 在使用os模块,需要先进行import操作: import os os模块中关于文件/目录常用的函数使用方法 函数名 函数作用 示例 ...
- chrome最强大的浏览器插件推荐,只要你会用其他的插件你可以删除了
我们在学习和工作中经常会需要用到各种各样不同需求的插件,结果chrome插件越装越多,chrome浏览器也越来越慢!有时候链我们自己都懵圈了,一时间都想不起来这个插件是干什么用的.更可气的是,很多时候 ...
- Matlab学习笔记(三)
二.MATLAB基础知识 (四)数组 MATLAB总是把数组看作存储和运算的基本单位,标量数据也被看作是(1×1)的数组 一维数组的创建 创建一维数组的几种方法:(e_two_14.m) 直接输入法: ...
- 【BZOJ 2118】 墨墨的等式(Dijkstra)
BZOJ2118 墨墨的等式 题链:http://www.lydsy.com/JudgeOnline/problem.php?id=2118 Description 墨墨突然对等式很感兴趣,他正在研究 ...
- Android SwipeSelector
Android SwipeSelector Android SwipeSelector是github上一个第三方开源的项目,其项目主页:https://github.com/roughike/S ...