• cs20_5-2


    1. Assignment_1

    1.1 1d解答

    1. tf.where()

      • 我的实践代码:

        def test_4_2():
            x = tf.constant([[True, False], [True, False]])
            x_where = tf.where(x) # Returns locations(coordinates ) of true values in a boolean tensor.
            # x中有两个True,所以返回了2个1-d tensor [],[] # 先确定几个True以确定返回几个坐标
            # 又因为x是2-d tensor, 所以定位True elem也要2-d indeies(行和列),即[0,0], [1,0] # 再确定坐标是什么
            print(sess.run(x_where))
            y = tf.constant(
                [
                    [
                        [True, False],[True, False]
                    ],
                    [
                        [False, True],[False, True]
                    ],
                    [
                        [False, False],[False, True]
                    ]
                ]
            )
            y_where = tf.where(y) # 5个True elme,所以返回5个坐标;又因为y是3-dim,所以坐标是                             3-d tensor(x,y,z)
            print(sess.run(y_where)) # [0,0,0], [0,1,0], [1,0,1], [1,1,1], [2,1,1]
        test_4_2()
        
      • 参考:

        [1] https://blog.csdn.net/A_a_ron/article/details/79048446 (讲得不错)

        [2] https://blog.csdn.net/ustbbsy/article/details/79564828

        [3] http://www.tensorfly.cn/tfdoc/api_docs/python/math_ops.html#where (还不错的第三方文档)

        [4] https://www.tensorflow.org/api_docs/python/tf/where (tf官方文档基本没有用,没有例子只有干巴巴的文字解释,晦涩难懂)

    2. tf.gater()

      • 我的示例代码

        def test_4_3():
            temp = tf.range(0, 10) * 10 + tf.constant(1, shape=[10])
            temp2 = tf.gather(temp, [1, 5, 9]) # 相当于根据indeies提取元素
            with tf.Session() as sess:
                print(sess.run(temp))
                print(sess.run(temp2))
        test_4_3()
        
        def test_4_4():
            a = tf.Variable([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]])
            index_a = tf.Variable([0, 2])
            b = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
            index_b = tf.Variable([2, 4, 6, 8])
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer()) # 变量要先初始化
                print(sess.run(tf.gather(a, index_a)))
                print(sess.run(tf.gather(b, index_b)))
                # [[ 1  2  3  4  5],[11 12 13 14 15]]
                #  [3 5 7 9]
        test_4_4()
        
      • 参考:

        [1] https://blog.csdn.net/guotong1988/article/details/53172882

        [2] https://blog.csdn.net/Cyiano/article/details/76087747

    3. tf.greater

      • 我的示例代码

        # 判断函数。首先张量x和张量y的尺寸要相同,输出的tf.greater(x, y)也是一个和x,y尺寸相同的张量。如果x的某个元素比y中对应位置的元素大,则tf.greater(x, y)对应位置返回True,否则返回False。与此类似的函数还有tf.greater_equal
        
        def test_4_5():
            x = tf.Variable([[1, 2, 3], [6, 7, 8], [11, 12, 13]])
            y = tf.Variable([[0, 1, 2], [5, 6, 7], [10, 11, 12]])
            x1 = tf.Variable([[1, 2, 3], [6, 7, 8], [11, 12, 13]])
            y1 = tf.Variable([[10, 1, 2], [15, 6, 7], [10, 21, 12]])
            with tf.Session() as sess:
                sess.run(tf.global_variables_initializer())
                print(sess.run(tf.greater(x, y)))
                print(sess.run(tf.greater(x1, y1)))
                # [[ True  True  True],[ True  True  True],[ True  True  True]]
                #  [[False  True  True],[False  True  True],[ True False  True]]
        test_4_5()
        
      • 参考:

        [1] https://blog.csdn.net/Cyiano/article/details/76087747 (例子简单易懂)

    4. 解决A1_1d:

      def test_4():
          x = tf.constant([29.05088806, 27.61298943, 31.19073486, 29.35532951,
                           30.97266006, 26.67541885, 38.08450317, 20.74983215,
                           34.94445419, 34.45999146, 29.06485367, 36.01657104,
                           27.88236427, 20.56035233, 30.20379066, 29.51215172,
                           33.71149445, 28.59134293, 36.05556488, 28.66994858], name="x")
          y = tf.ones(shape=x.shape, dtype=tf.float32) # 构造与x相同shape的tensor
          y = tf.multiply(y, tf.constant(30.0))
          print("y: ", sess.run(y))
          x_bool = tf.greater(x,y,name="x_bool") # 获得一个bool tensor
          x_index = tf.where(x_bool) # 获得 x>30的elme index
          out = tf.gather(x,x_index) # 根据x>30的indexies再从x中取elems
          print("test4-x: ", sess.run(x))
          print("test4-x>30: ", sess.run(out))
      test_4()
      
    5. 拓展:tf其他几个常见函数:

      • tf.concat

        # 把一组向量从某一维上拼接起来,很向numpy中的Concatenate,官网例子:
        
        t1 = [[1, 2, 3], [4, 5, 6]]
        t2 = [[7, 8, 9], [10, 11, 12]]
        tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
        tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]
        
        # tensor t3 with shape [2, 3]
        # tensor t4 with shape [2, 3]
        tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3]
        tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6]
        
        # 其实,如果是list类型的话也是可以的,只要是形似Tensor,最后tf.concat返回的还是Tensor类型
        
      • tf.gather_nd

        # tf.gather(): 类似于数组的索引,可以把向量中某些索引值提取出来,得到新的向量,适用于要提取的索引为不连续的情况。这个函数似乎只适合在一维的情况下使用。
        # gather_nd同上,但允许在多维上进行索引,例子只展示了一种很简单的用法,更复杂的用法可见官网。
        
        import tensorflow as tf 
        
        a = tf.Variable([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]])
        index_a = tf.Variable([[0,2], [0,4], [2,2]])
        
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            print(sess.run(tf.gather_nd(a, index_a)))
        #  [ 3  5 13]
        
      • tf.cast

        # 转换数据类型。
        
        a = tf.constant([0, 2, 0, 4, 2, 2], dtype='int32')
        print(a)
        # <tf.Tensor 'Const_1:0' shape=(6,) dtype=int32>
        
        b = tf.cast(a, 'float32')
        print(b)
        # <tf.Tensor 'Cast:0' shape=(6,) dtype=float32>
        
      • tf.expand_dims & tf.squeeze

        # 增加 / 压缩张量的维度。
        
        a = tf.constant([0, 2, 0, 4, 2, 2], dtype='int32')
        print(a)
        # <tf.Tensor 'Const_1:0' shape=(6,) dtype=int32>
        
        b = tf.expand_dims(a, 0)
        print(b)
        # <tf.Tensor 'ExpandDims:0' shape=(1, 6) dtype=int32>
        
        print(tf.squeeze(b, 0))
        # <tf.Tensor 'Squeeze:0' shape=(6,) dtype=int32>
        
      • 参考:

        [1] https://blog.csdn.net/Cyiano/article/details/76087747 (例子简单易懂)

        [2] https://www.tensorflow.org/versions/r1.1/api_docs/python/tf/concat (明白了:官网文档的某些api有详细例子,某些没有就很坑)

    1.2 其他题解答

    1. tf.diag

      • 示例代码

        def test5(): # 这也是1e
            diag = tf.range(1,7) # [1,7)
            print("test_5: ", sess.run(tf.diag(diagonal=diag)))
        test5()
        
        def test5_2():
            diag = tf.constant([1,2,3,5])
            print("test_5_2: ", sess.run(tf.diag(diagonal=diag)))
        test5_2()
        
        # 其实diagonal还能是 2-d/3-d tensor, 详见下方link/官方api
        
      • 参考:

        [1] https://blog.csdn.net/baidu_15113429/article/details/78082282

    2. tf.matrix_determinant

      • 示例

        def test6():
            x = tf.random_normal([10,10], mean=0.5, stddev=1.5)
            print("test6, x: ", sess.run(x))
            print("test6, det(x): ", sess.run(tf.matrix_determinant(x)))
        test6()
        
        def test6_2():
            A1 = [[1, 1, 1], [1, -1, -1], [5, -2, 2]]
            A = tf.constant(A1, tf.float32)
            print(sess.run(A))
            d = tf.matrix_determinant(A)
            print("det(d): ", sess.run(d))
        test6_2()
        
      • 参考

        [1] https://segmentfault.com/a/1190000014591047 (包含很多矩阵的基本操作)

    3. tf.unique

      • 代码示例

        def test7():
            x = tf.constant([5, 2, 3, 5, 10, 6, 2, 3, 4, 2, 1, 1, 0, 9])
            y, idx = tf.unique(x) # y是去重之后的1-d tensor, idx是原来x的index,只不过
            # x中重复的元素的index被换成该重复元素第一次出现的index
            print("test7: idx: {}, x: {}".format(sess.run(y), sess.run(idx)))
        test7()
        
      • 参考

        [1] https://www.tensorflow.org/api_docs/python/tf/unique (终于碰到一个api的官方文档有例子了)

    4. tf.reduce_mean

    5. 最后一个作业,基于tf.reduce_mean,sum, tf.cond,有点像实现huber_loss

      def test8():
          x, y = tf.random_normal([300]), tf.random_normal([300])
          print("test8, 
       x:{} 
       y:{}".format(sess.run(x), sess.run(y)))
          residual = x - y # 返回一个tensor(shape同x,y)
          mean_residual = tf.reduce_mean(residual)
          #
          def f1(): return tf.reduce_mean(tf.square(residual)) # 默认使用上述的全局residual
          def f2(): return tf.reduce_sum(tf.abs(residual))
          return tf.cond(mean_residual < 0, f1, f2)
      res = test8()
      print("test8, res is:{}".format(sess.run(res)))
      
    6. 上述这些小题目的官方solutions:

      https://github.com/chiphuyen/stanford-tensorflow-tutorials/blob/master/assignments/01/q1_sol.py

    1.3 LR_MNIST

    1. 以前写过,有时间再写一次

    1.4 LR_notMNIST

    1. 一个像MNIST的新数据集:notMNIST
    2. 有时间写一下

    1.5 实现97%以上的MNIST的acc

    1. 参考:http://rodrigob.github.io/are_we_there_yet/build/classification_datasets_results.html
    2. 有时间写一下

    2. CNN部分实践

    2.1 一个toy example

    1. 网络图:

    2. 一些准备

      • ubuntu 16.04 + conda + py3.6 + opencv: https://www.jianshu.com/p/a1db7533b5da
      • python/tf中的 -1 or None大多数情况下指的是:程序自动推断这个值是多少(-1 or None只是一个“自动推断”的标识符)
      • if i % int(data.train.num_examples/batch_size) == 0:表示刚执行完一个epoch
      • 数据集问题已解决(通过kaggle),但是代码风格不太喜欢(有点旧了),抽时间再完成这个

    2.2 参考

    3. Assignment_2

    • 参见7_style_transfer
  • 相关阅读:
    JSP第七次作业
    JSP第六次作业
    Jsp第五次作业
    软件测试第二次作业
    Jsp第四次作业2
    Jsp第四次作业1
    软件测试第一次作业
    JSP第三次作业
    JSP第二次作业
    第七次作业
  • 原文地址:https://www.cnblogs.com/LS1314/p/10371197.html
Copyright © 2020-2023  润新知