# -*- coding: utf-8 -*-

import tensorflow as tf
w1=tf.Variable(tf.random_normal([2,6],stddev=1))
w2=tf.Variable(tf.random_normal([6,1],stddev=1))

x=tf.placeholder(dtype=tf.float32,shape=(4,2),name="input")
h=tf.matmul(x,w1)
y=tf.matmul(h,w2)

init_op=tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)

print sess.run(y,feed_dict={x:[[5.2,2.9],[3.9,1.1],[3.9,5.2],[6.1,9.2]]})

数据需要通过字典输入

# Launch the graph in a session.
with tf.Session() as sess:
    # Run the variable initializer.
    sess.run(w.initializer)
    # ...you now can run ops that use the value of 'w'...

#global_variables_initializer()to add an Op to the graph that initializes all the variables. You then run that Op after launching the graph.Add an Op to initialize global variables.
init_op = tf.global_variables_initializer()

# Launch the graph in a session.
with tf.Session() as sess:
    # Run the Op that initializes global variables.
    sess.run(init_op)
    # ...you can now run any Op that uses variable values...

tf.Variable

_init__(
    initial_value=None,
    trainable=True,
    collections=None,
    validate_shape=True,
    caching_device=None,
    name=None,
    variable_def=None,
    dtype=None,
    expected_shape=None,
    import_scope=None
)

Creates a new variable with value initial_value.

The new variable is added to the graph collections listed in collections, which defaults to [GraphKeys.GLOBAL_VARIABLES].

If trainable is True the variable is also added to the graph collection GraphKeys.TRAINABLE_VARIABLES.

This constructor creates both a variable Op and an assign Op to set the variable to its initial value.

Args:

  • initial_value: A Tensor, or Python object convertible to a Tensor, which is the initial value for the Variable. The initial value must have a shape specified unless validate_shape is set to False. Can also be a callable with no argument that returns the initial value when called. In that case, dtype must be specified. (Note that initializer functions from init_ops.py must first be bound to a shape before being used here.)
  • trainable: If True, the default, also adds the variable to the graph collection GraphKeys.TRAINABLE_VARIABLES. This collection is used as the default list of variables to use by the Optimizer classes.
  • collections: List of graph collections keys. The new variable is added to these collections. Defaults to [GraphKeys.GLOBAL_VARIABLES].
  • validate_shape: If False, allows the variable to be initialized with a value of unknown shape. If True, the default, the shape of initial_value must be known.
  • caching_device: Optional device string describing where the Variable should be cached for reading. Defaults to the Variable's device. If not None, caches on another device. Typical use is to cache on the device where the Ops using the Variable reside, to deduplicate copying through Switch and other conditional statements.
  • name: Optional name for the variable. Defaults to 'Variable' and gets uniquified automatically.
  • variable_defVariableDef protocol buffer. If not None, recreates the Variable object with its contents, referencing the variable's nodes in the graph, which must already exist. The graph is not changed.variable_def and the other arguments are mutually exclusive.
  • dtype: If set, initial_value will be converted to the given type. If None, either the datatype will be kept (if initial_value is a Tensor), or convert_to_tensor will decide.
  • expected_shape: A TensorShape. If set, initial_value is expected to have this shape.
  • import_scope: Optional string. Name scope to add to the Variable. Only used when initializing from protocol buffer.

Raises:

  • ValueError: If both variable_def and initial_value are specified.
  • ValueError: If the initial value is not specified, or does not have a shape and validate_shape is True.

tf随笔-5的更多相关文章

  1. TF随笔-13

    import tensorflow as tf a=tf.constant(5) b=tf.constant(3) res1=tf.divide(a,b) res2=tf.div(a,b) with ...

  2. TF随笔-11

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- import tensorflow as tf my_var=tf.Variable(0.) step=t ...

  3. TF随笔-10

    #!/usr/bin/env python# -*- coding: utf-8 -*-import tensorflow as tf x = tf.constant(2)y = tf.constan ...

  4. TF随笔-9

    计算累加 #!/usr/bin/env python2 # -*- coding: utf-8 -*-"""Created on Mon Jul 24 08:25:41 ...

  5. TF随笔-8

    #!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Mon Jul 10 09:35:04 201 ...

  6. TF随笔-7

    求平均值的函数 reduce_mean axis为1表示求行 axis为0表示求列 >>> xxx=tf.constant([[1., 10.],[3.,30.]])>> ...

  7. tf随笔-6

    import tensorflow as tfx=tf.constant([-0.2,0.5,43.98,-23.1,26.58])y=tf.clip_by_value(x,1e-10,1.0)ses ...

  8. TF随笔-4

    >>> import tensorflow as tf>>> a=tf.constant([[1,2],[3,4]])>>> b=tf.const ...

  9. TF随笔-3

    >>> import tensorflow as tf>>> node1 = tf.constant(3.0, dtype=tf.float32)>>& ...

随机推荐

  1. Linux指令od和hexdump

    Linux指令:od (octal dump) 示例用法:od -c hello Linux指令:od od命令用户通常使用od命令查看特殊格式的文件内容.通过指定该命令的不同选项可以以十进制.八进制 ...

  2. xshell下载安装

    打开网址http://www.netsarang.com/download/software.html 找到最新版的xshell,点击下载 在跳转的页面填写个人信息,许可证类型选择家庭和学校使用,除了 ...

  3. POJ 1659 Frogs' Neighborhood (Havel定理构造图)

    题意:根据图的度数列构造图 分析:该题可根据Havel定理来构造图.Havel定理对可图化的判定: 把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d ...

  4. js小技巧(收集的)

    一.事件源对象 event.srcElement.tagName //IE浏览器 event.srcElement.type event.target.tagName //dom浏览器 event.t ...

  5. Jsoup学习总结

    Jsoup学习总结 摘要 Jsoup是一款比较好的Java版HTML解析器.可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方 ...

  6. sql中1=1和1=0的用处

    where 1=1 where 1=1有什么用?在SQL语言中,写这么一句话就跟没写一样. select * from table1 where 1=1与select * from table1完全没 ...

  7. Android bluetooth介绍

    Android bluetooth介绍(一):基本概念及硬件接口Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcomm流程Android blueto ...

  8. MyEclipse激活失败

    最近从MyEclipse2014升级MyEclipse2015,结果按照MyEclipse2014的方式激活2015总是失败,显示错误如下图所示: 反复实验,怎么也不能成功激活,最终找到方法 很多情况 ...

  9. Linux系统crontab定时调度Python脚本

    Linux系统crontab定时调度Python脚本 一.Python脚本随Linux开机自动运行 #Python脚本:/home/edgar/auto.py #用root权限编辑以下文件:/etc/ ...

  10. Apache 防盗链配置

    盗链 网站内有许多的图片地址,或一些我们可以用到的资源,在这种情况下,我可以通过赋值其他图片链接地址,到我自己的平台上,这样相当于盗取了一张图片的链接,那么盗链会有什么危害呢? 当我们盗取一张图片链接 ...