• 陈浩然
    2019-01-14
    import tensorflow as tf

    W = tf.Variable(initial_value = tf.random_normal(shape = (1 , 4) ,mean = 100 , stddev = 0.35 ), name = "W")
    b = tf.Variable(tf.zeros([4]) , name="b")
    [W , b]
    [<tf.Variable 'W:0' shape=(1, 4) dtype=float32_ref>,
     <tf.Variable 'b:0' shape=(4,) dtype=float32_ref>]

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    sess.run( [W , b])
    [array([[100.40549 , 99.182625, 100.23513 , 99.42683 ]], dtype=float32),
     array([0., 0., 0., 0.], dtype=float32)]

    sess.run(tf.assign_add(b , [1,1,1,1] ))

    sess.run(tf.assign_add(b , [1,1,1,1] ))
    ​
    array([1., 1., 1., 1.], dtype=float32)

    sess.run(b)
    array([1., 1., 1., 1.], dtype=float32)

    saver = tf.train.Saver({'W' : W , 'b' : b})
    saver.save(sess , './summary/data.ckpt' , global_step = 0)
    WARNING:tensorflow:Issue encountered when serializing trainable_variables.
    Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore.
    'test' has type str, but expected one of: int, long, bool
    WARNING:tensorflow:Issue encountered when serializing variables.
    Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore.
    'test' has type str, but expected one of: int, long, bool
    './summary/data.ckpt-0'

    sess.run(tf.assign_add(b , [1,1,1,1] ))
    sess.run(b)
    array([2., 2., 2., 2.], dtype=float32)

    saver.restore(sess , './summary/data.ckpt-0')

    saver.restore(sess , './summary/data.ckpt-0')
    sess.run(b)
    INFO:tensorflow:Restoring parameters from ./summary/data.ckpt-0
    array([1., 1., 1., 1.], dtype=float32)
    展开
    
     2
  • bearlu
    2019-01-12
    老师,麻烦贴一下源码地址

    作者回复: https://github.com/DjangoPeng/tensorflow-101

    
     2
  • ttttt
    2020-01-13
    2.0的朋友找到解决方案了,为了成功运行老师的代码:开头可以这样
    import tensorflow.compat.v1 as tf
    tf.disable_v2_behavior()
    
    
  • ttttt
    2020-01-13
    注意TensorFlow2.0 相对于1.0很多都改名了。

    编辑回复: 是的

    
    
  • ttttt
    2020-01-13
    注意
    tf.random_normal方法改名了
    改成了tf.random.normal
    # 创建变量
    # tf.random_normal 方法返回形状为(1,4)的张量。它的4个元素符合均值为100、标准差为0.35的正态分布。
    W = tf.Variable(initial_value=tf.random.normal(shape=(1, 4), mean=100, stddev=0.35), name='W')
    b = tf.Variable(tf.zeros([4]), name='b')
    展开
    
    
  • ZeroIce
    2019-08-24
    老师,有什么书籍推荐看的?英文系列也行

    作者回复: 《Python深度学习》Keras作者写的
    《深入理解TensorFlow》也可以看看

    
    
  • pyw
    2019-08-08
    从文件恢复的时候出错:
    ```
    WARNING: Logging before flag parsing goes to stderr.
    W0808 00:16:28.798274 140416759756608 deprecation.py:323] From /home/pyw/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version.
    Instructions for updating:
    Use standard file APIs to check for files with this prefix.
    Traceback (most recent call last):
      File "c20.py", line 39, in <module>
        saver.restore(sess, './save/1.save')
      File "/home/pyw/.local/lib/python3.6/site-packages/tensorflow/python/training/saver.py", line 1278, in restore
        compat.as_text(save_path))
    ValueError: The passed save_path is not a valid checkpoint: ./save/1.save
    ```

    c20.py的39行是:
    saver.restore(sess, './save/1.save')
    展开
    
    
  • pyw
    2019-08-07
    有的方法过期了
    ```
    W0807 23:51:48.566468 140651389855552 deprecation_wrapper.py:119] From c20.py:14: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
    W0807 23:51:48.864450 140651389855552 deprecation_wrapper.py:119] From c20.py:15: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead.
    W0807 23:51:49.436012 140651389855552 deprecation_wrapper.py:119] From c20.py:19: The name tf.assign_add is deprecated. Please use tf.compat.v1.assign_add instead.
    ```

    如果按照提示修改,会得到

    ```
        sess.run(tf.compat.v1.global_variables_initializer())
    TypeError: unbound method run() must be called with Session instance as first argument (got Operation instance instead)
    ```
    展开
     1
    
  • 沉枫y
    2019-02-26
    接上一条留言
    print(sess.run(b))
    print(b) 两者的输出结果不一样
    [1. 1. 1. 1.]
    <tf.Variable 'b:0' shape=(4,) dtype=float32_ref>
    是不是可以理解为 b的本身只是一个定义好的30位的浮点数张量
    在经过session会话后,只是在该会话改变了他的值,但并不改变他本身的属性
    还是以上理解是错的 是其他的解释
    展开

    作者回复: 在该会话(上下文)中改变(初始化、计算、赋值)了它。如果你关闭会话 ,它还是一个数据流图中的Variable 实例。

    
    
  • 沉枫y
    2019-02-26
    执行完sess.run([W, b])后 去打印变量W后的结果并没有发生变化(在IDLE中操作)

    作者回复: print(sess.run([W, b])) 会打印出结果,因为会话执行特定节点(变量、操作)返回的是 python 数据类型。但是 W和b本身是 TensorFlow DSL 定义的数据类型,是一个抽象的“壳”。无论你执行多少次,“壳”是不会变得。

    
    
  • 稀饭ETF
    2019-01-13
    终于赶上进度了😂

    作者回复: 加油加油!

    
    
我们在线,来聊聊吧