美文网首页数据科学家
TensorFlow: Basic Exercises (更新中

TensorFlow: Basic Exercises (更新中

作者: Deepool | 来源:发表于2017-02-20 23:14 被阅读1218次

    Note: 以下绝大部分内容来自 Kyubyong 的GitHub

    **-

    Part 1:Constants, Sequences, and Random Values

    import numpy as np
    from scipy import misc
    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    sess = tf.InteractiveSession()
    """
    Ex1. Create a tensor of the shape [2, 3] with all elements set to zero.
    """
    X = tf.zeros([2,3])
    print (X.eval())
    
    """
    Ex2. Let x be a tensor of [[1,2,3], [4,5,6]]. 
         Create a tensor of the same shape and dtype as x with all elements set to zero.
    """
    x = tf.convert_to_tensor(np.array([[1,2,3],[4,5,6]]))
    # x = tf.constant([[1,2,3],[4,5,6]])
    X = tf.zeros_like(x)
    print (X.eval())
    
    """
    Ex3. Create a tensor of the shape [3, 2], with all elements of 5.
    """
    out1 = tf.ones([3,2])*5
    out2 = tf.fill([3,2],5)
    print (out1.eval())
    print (out2.eval())
    
    """
    Ex4. Create a constant tensor of [[1, 3, 5], [4, 6, 8]].
    """
    out = tf.constant([[1,3,5],[4,6,8]])
    print (out.eval())
    
    """
    Ex5. Create a constant tensor of the shape [2, 3], with all elements set to 4.
    """
    out = tf.constant(4, shape=[2,3])
    print (out.eval())
    
    """
    Ex6. Create a 1-D tensor of 50 evenly spaced elements between 5 and 10 inclusive.
    """
    out = tf.linspace(5.0,10.0,50)
    print (out.eval())
    
    """
    Ex7. Create a tensor which looks like [10, 12, 14, 16, ..., 100].
    """
    out = tf.range(10, limit=102, delta=2)          # [10, 102)
    print (out.eval())    
    
    """
    Ex8. Create a random tensor of the shape [3, 2], with elements from a normal distribution of mean=0, standard deviation=2.
    """
    out = tf.random_normal([3,2], mean=0, stddev=2)
    print (out.eval())
    
    """
    Ex9. Create a random tensor of the shape [3, 2], with all elements from a uniform distribution that ranges from 0 to 2 (exclusive).
    """
    out = tf.random_uniform([3,2], minval=0, maxval=2)      # [minval, maxval)
    print (out.eval())
    
    """
    Ex10. Let x be a tensor of [[1, 2], [3, 4], [5, 6]]. Shuffle x along its first dimension.
    """
    x = tf.constant([[1,2],[3,4],[5,6]])
    out = tf.random_shuffle(x)
    print (x.eval())
    print (out.eval())
    
    """
    Ex11. Let x be a random tensor of the shape [10, 10, 3], with elements from a unit normal distribution.
    Crop x with the size of [5, 5, 3].
    """
    x = tf.random_normal([10,10,3])
    out = tf.random_crop(x, [5,5,3])
    print (out.eval())
    
    # An image example
    img = misc.imread('cat.png')
    im = tf.Variable(img)
    im_ = tf.random_crop(im, [100,100,3])
    init = tf.global_variables_initializer()
    sess.run(init)
    ims = im.eval()
    ims_ = im_.eval()
    plt.subplot(1,2,1)
    plt.imshow(ims)
    plt.subplot(1,2,2)
    plt.imshow(ims_)
    plt.show()
    
    Randomly crop

    Part 2:Control Flow

    import numpy as np
    import tensorflow as tf
    
    """
    Ex1. Let x and y be random 0-D tensors. Return x + y if x < y and x - y otherwise.
    """
    x = tf.random_uniform([])
    y = tf.random_uniform([])
    out = tf.cond(tf.less(x,y), lambda: tf.add(x,y), lambda: tf.sub(x,y))
    print (out.eval())
    
    
    """
    Ex2. Let x and y be 0-D int tensors randomly selected from 0 to 5. Return x + y if x < y, x - y elif x > y, 0 otherwise.
    """
    x = tf.random_uniform([], minval=0, maxval=5, dtype=tf.int32)
    y = tf.random_uniform([], minval=0, maxval=5, dtype=tf.int32)
    out = tf.case({tf.less(x,y): lambda: tf.add(x,y), tf.greater(x,y): lambda: tf.sub(x,y)},
                  default=lambda: tf.constant(0), exclusive=True)
    print (out.eval())
    
    # Alternative
    x = np.random.randint(0, 5)
    y = np.random.randint(0, 5)
    out = np.select([x > y, x < y, x == y], [x + y, x - y, 0])
    print (out)
    
    
    """
    Ex3. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x.
         Return a boolean tensor that yields Trues if x equals y element-wise.
    """
    x = tf.constant([[-1,-2,-3], [0,1,2]])
    y = tf.zeros_like(x)
    out = tf.equal(x, y)
    print (out.eval())
    
    
    """
    Ex4. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x.
         Return a boolean tensor that yields Trues if x does not equal y element-wise.
    """
    x = tf.constant([[-1,-2,-3], [0,1,2]])
    y = tf.zeros_like(x)
    out = tf.not_equal(x, y)
    print (out.eval())
    
    
    """
    Ex5. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x.
         Return a boolean tensor that yields Trues if x is greater than or equal to y element-wise.
    """
    x = tf.constant([[-1,-2,-3], [0,1,2]])
    y = tf.zeros_like(x)
    out = tf.greater_equal(x, y)
    print (out.eval())
    
    
    """
    Ex6. Let x be a tensor [[-1, -2, -3], [0, 1, 2]] and y be a tensor of zeros with the same shape as x.
         Return a boolean tensor that yields Trues if x is less than or equal to y elementwise.
    """
    x = tf.constant([[-1,-2,-3], [0,1,2]])
    y = tf.zeros_like(x)
    out = tf.less_equal(x, y)
    print (out.eval())
    
    
    """
    Ex7. Let x be a 0-D tensor randomly selected from -5 to 5. Return a boolean tensor that yields Trues if x
         is less than 3 and x is greater than 0.
    """
    x = tf.random_uniform([], minval=-5, maxval=5)
    out = tf.logical_and(x<3, x>0)
    print (out.eval())
    
    
    """
    Ex8. Let x be a tensor [[1, 2], [3, 4]], y be a tensor [[5, 6], [7, 8]], and z be a boolean tensor
         [[True, False], [False, True]]. Create a 2*2 tensor such that each element corresponds to x if True, otherise y.
    """
    x = tf.constant([[1,2],[3,4]])
    y = tf.constant([[5,6],[7,8]])
    z = tf.constant([[True,False], [False,True]])
    out = tf.select(z, x, y)
    print (out.eval())
    
    
    """
    Ex9. Let x be a tensor [1, 2, 3, ..., 100]. Extract elements of x greater than 30.
    """
    x = tf.range(1, 101)
    out = tf.gather_nd(x, tf.where(x>30))
    print (out.eval())
    

    Part 3:Math

    import numpy as np
    import tensorflow as tf
    
    print (tf.__version__)      # 1.0.0
    
    # ------  Arithmetic Operators  ------ #
    
    """
    Ex1. Add x and y element-wise.
    """
    x = tf.constant([1,2,3])
    y = tf.constant([4,5,6])
    out = tf.add(x,y)
    # out = x + y
    print (out.eval())
    
    """
    Ex2. Subtract y from x element-wise.
    """
    x = tf.constant([1,2,3])
    y = tf.constant([4,5,6])
    out = tf.subtract(x,y)
    # out = x - y
    print (out.eval())
    
    """
    Ex3. Multiply x by y element-wise.
    """
    x = tf.constant([1,2,3])
    y = tf.constant([4,5,6])
    out = tf.multiply(x,y)
    # out = x * y
    print (out.eval())
    
    """
    Ex4. Multiply x by 5 element-wise.
    """
    x = tf.constant([1,2,3])
    out = tf.scalar_mul(5, x)
    # out = 5 * x
    print (out.eval())
    
    """
    Ex5. Predict the result of this.
    """
    x = tf.constant([9,11,13])
    y = tf.constant([4,5,6])
    out1 = tf.div(x,y)         # out1 = x//y
    out2 = tf.truediv(x,y)     # out2 = x/y
    print (out1.eval())
    print (out2.eval())
    
    """
    Ex6. Get the remainder of x / y element-wise.
    """
    x = tf.constant([9,11,13])
    y = tf.constant([4,5,6])
    out = tf.mod(x,y)
    # out = x % y
    print (out.eval())
    
    """
    Ex7. Compute the pairwise cross product of x and y.
    """
    x = tf.constant([1,2,3])
    y = tf.constant([4,5,6])
    out = tf.cross(x, y)        # tf.cross == np.cross
    print (out.eval())
    
    
    # ------ Basic Math Functions ------ #
    
    """
    Ex8. Add x, y, and z element-wise.
    """
    x = tf.constant([1,2,3])
    y = tf.constant([4,5,6])
    z = tf.constant([7,8,9])
    out = tf.add_n([x,y,z])
    # out = x + y + z
    print (out.eval())
    
    """
    Ex9. Compute the absolute value of x.
    """
    x = tf.constant([[-1,-2,-3],[4,-5,6]])
    out = tf.abs(x)
    print (out.eval())
    
    """
    Ex10. Compute numerical negative value of x, elemet-wise.
    """
    x = tf.constant([[-1,-2,-3],[4,-5,6]])
    out = tf.negative(x)
    # out = -x
    print (out.eval())
    
    """
    Ex11. Compute an element-wise indication of the sign of x, element-wise.
    """
    x = tf.constant([-5,-3,0,3,5])
    out = tf.sign(x)
    print (out.eval())
    
    """
    Ex12. Compute the reciprocal of x, element-wise.
    """
    x = tf.constant([1,2,3], dtype=tf.float32)
    out = tf.reciprocal(x)
    # out = 1/x
    print (out.eval())
    
    """
    Ex13. Compute the square of x, element-wise.
    """
    x = tf.constant([1,2,-3])
    out = tf.square(x)
    #out = x**2
    print (out.eval())
    
    """
    Ex14. Predict the results of this, paying attention to the difference among the family functions.
    """
    x = tf.constant([2.1, 1.5, -2.5, -2.9])
    out1 = tf.round(x)      # Rounds the values of a tensor to the nearest integer, element-wise.
    out2 = tf.floor(x)      # Returns element-wise largest integer not greater than x.
    out3 = tf.ceil(x)       # Returns element-wise smallest integer in not less than x.
    print (out1.eval())
    print (out2.eval())
    print (out3.eval())
    
    """
    Ex15. Compute square root of x element-wise.
    """
    x = tf.constant([1., 2., 3., 4.], dtype=tf.float64)
    # x = tf.constant([1., 2., 3., 4.])   # default: float32
    out = tf.sqrt(x)       # Note: in TensorFlow, the input tensor must be a float, whereas in Numpy, an integer is okay.
    print (out.eval())
    
    """
    Ex16. Compute the reciprocal of square root of x element-wise.
    """
    x = tf.constant([1.0,4,9])
    out = tf.rsqrt(x)
    print (out.eval())
    
    """
    Ex17. Compute x^y, element-wise.
    """
    x = tf.constant([[1,2,3],[4,5,6]])
    y = tf.constant([[1,2,1],[2,1,2]])
    out = tf.pow(x,y)         # tf.pow == np.power
    print (out.eval())
    
    """
    Ex18. Compute e^x, element-wise.
    """
    x = tf.constant([[1,2,3],[4,5,6]], dtype=tf.float32)
    out = tf.exp(x)          # tf.exp == np.exp
    # out = tf.pow(np.e, x)
    print (out.eval())
    
    """
    Ex19. Compute natural logarithm of x element-wise.
    """
    x = tf.constant([1, np.e, np.e**2])
    out = tf.log(x)       # tf.log == np.log
    print (out.eval())
    
    """
    Ex20. Compute the max of x and y element-wise.
    """
    x = tf.constant([1,3,5])
    y = tf.constant([3,1,7])
    out = tf.maximum(x, y)
    # out = tf.where(x>y, x, y)
    print (out.eval())
    
    """
    Ex21. Compute the min of x and y element-wise.
    """
    x = tf.constant([1,3,5])
    y = tf.constant([3,1,7])
    out = tf.minimum(x, y)
    # out = tf.where(x<y, x, y)
    print (out.eval())
    
    """
    Ex22. Compuete the sine, cosine, and tangent of x, element-wise.
    """
    x = tf.constant([-np.pi, np.pi, np.pi/2.0], dtype=tf.float64)
    out1 = tf.sin(x)
    out2 = tf.cos(x)
    out3 = tf.tan(x)
    print (out1.eval())
    print (out2.eval())
    print (out3.eval())
    
    """
    Ex23. Compute (x - y)(x - y) element-wise.
    """
    x = tf.constant([2, 3, 4])
    y = tf.constant([1, 5, 1])
    out = tf.squared_difference(x,y)
    # out = tf.square(tf.subtract(x, y))
    print (out.eval())
    
    
    # ------ Matrix Math Functions ------ #
    
    """
    Ex1. Create a diagonal tensor with the diagonal values of x.
    """
    x = tf.constant([1,2,3,4])
    out = tf.diag(x)
    print (out.eval())
    
    """
    Ex2. Extract the diagonal of x.
    """
    x = tf.constant([[1,0,0,0],[0,2,0,0],[0,0,3,0],[0,0,0,4]])
    out = tf.diag_part(x)
    print (out.eval())
    
    """
    Ex3. Permutate the dimensions of x (2, 3, 4) such that the new tensor has shape (3, 4, 2).
    """
    x = tf.random_normal([2,3,4])
    out = tf.transpose(x, [1,2,0])
    print (out.get_shape())
    
    """
    Ex4. Construct a 3 by 3 identity matrix.
    """
    out = tf.eye(3)
    print (out.eval())
    
    """
    Ex5. Predict the result of this.
    """
    x = tf.constant([[1,2,3,4],[5,6,7,8]])
    out1 = tf.matrix_diag(x)
    out2 = tf.matrix_diag_part(out1)
    print (out1.eval())
    print (out2.eval())
    
    """
    Ex6. Transpose the last two dimensions of x.
    """
    x = tf.random_normal([2,3,4])
    out = tf.matrix_transpose(x)
    # out = tf.transpose(x, [0,2,1])
    print (out.eval().shape)
    
    """
    Ex7. Multiply x by y.
    """
    x = tf.constant([[1,2,3], [4,5,6]])
    y = tf.constant([[1,1], [2,2], [3,3]])
    out = tf.matmul(x,y)
    print (out.eval())
    
    """
    Ex8. Multiply slices of x and y in batches.
    """
    x = tf.constant([[[1,2,3],[4,5,6]], [[1,2,3],[4,5,6]]])
    y = tf.constant([[[1,1],[2,2],[3,3]], [[1,1],[2,2],[3,3]]])
    out = tf.matmul(x,y)
    print (out.eval())
    
    """
    Ex9. Compute the determinant of x.
    """
    _x = np.arange(1, 5, dtype=np.float32).reshape((2, 2))
    x = tf.convert_to_tensor(_x)
    out = tf.matrix_determinant(x)
    print (out.eval())
    
    """
    Ex10. Compute the inverse of x.
    """
    x = tf.constant([[1,2],[3,4]], dtype=tf.float64)
    out = tf.matrix_inverse(x)
    print (out.eval())
    
    """
    Ex11. Compute the eigenvalues and eigenvectors of x.
    """
    x = tf.diag(tf.constant([1,2,3], dtype=tf.float32))
    eigenvals, eigenvecs = tf.self_adjoint_eig(x)
    print (eigenvals.eval())
    print (eigenvecs.eval())
    
    """
    Ex12. Compute the singular values of x.
    """
    x = tf.constant([[1, 0, 0, 0, 2],[0, 0, 3, 0, 0],[0, 0, 0, 0, 0],[0, 2, 0, 0, 0]], dtype=tf.float32)
    s = tf.svd(x, compute_uv=False)
    print (s.eval())
    
    
    # ------ Reduction ------ #
    
    """
    Ex13. Predict the results of these.
    """
    x = tf.constant([[1, 2, 3, 4],[5, 6, 7, 8]])
    outs = [tf.reduce_sum(x),
            tf.reduce_sum(x, axis=0),
            tf.reduce_sum(x, axis=1, keep_dims=True),
            "",
            tf.reduce_prod(x),
            tf.reduce_prod(x, axis=0),
            tf.reduce_prod(x, axis=1, keep_dims=True),
            "",
            tf.reduce_min(x),
            tf.reduce_min(x, axis=0),
            tf.reduce_min(x, axis=1, keep_dims=True),
            "",
            tf.reduce_max(x),
            tf.reduce_max(x, axis=0),
            tf.reduce_max(x, axis=1, keep_dims=True),
            "",
            tf.reduce_mean(x),
            tf.reduce_mean(x, axis=0),
            tf.reduce_mean(x, axis=1, keep_dims=True)]
    
    for out in outs:
        if out == "": print ()
        else:
            print ("->", out.eval())
    
    """
    Ex14. Predict the results of these.
    """
    x = tf.constant([[True,True],[False,False]])
    outs = [tf.reduce_all(x),           # logical and
            tf.reduce_all(x, axis=0),
            tf.reduce_all(x, axis=1, keep_dims=True),
            "",
            tf.reduce_any(x),           # logical or
            tf.reduce_any(x, axis=0),
            tf.reduce_any(x, axis=1, keep_dims=True),
            ]
    
    for out in outs:           # If you remove the common suffix "reduce_", you will get the same result in numpy.
        if out == "": print ()
        else: print ("->", out.eval())
    
    """
    Ex15. Predict the results of these.
    """
    x = tf.constant([[0,1,0],[1,1,0]])
    outs = [tf.count_nonzero(x),
            tf.count_nonzero(x, axis=0),
            tf.count_nonzero(x, axis=1, keep_dims=True)]
    
    for out in outs:
        print ("->", out.eval())
    
    """
    Ex16. Complete the einsum function that would yield the same result as the given function.
    """
    _x = np.arange(1, 7).reshape((2, 3))
    _y = np.arange(1, 7).reshape((3, 2))
    
    x = tf.convert_to_tensor(_x, dtype=tf.float32)
    y = tf.convert_to_tensor(_y, dtype=tf.float32)
    
    # Matrix multiplication
    out1 = tf.einsum('ij,jk->ik', x, y)
    out1_ = tf.matmul(x, y)
    assert np.allclose(out1.eval(), out1_.eval())
    
    # Dot product
    flattened = tf.reshape(x, [-1])              # shape: (6,)
    out2 = tf.einsum('i,i->', flattened, flattened)
    out2_ = tf.reduce_sum(tf.multiply(flattened, flattened))
    assert np.allclose(out2.eval(), out2_.eval())
    
    # Outer product
        """
        The operation tf.expand_dims() is useful if you want to add a batch dimension to 
        a single element. For example, if you have a single image of shape 
        [height, width, channels], you can make it a batch of 1 image with 
        expand_dims(image, 0), which will make the shape [1, height, width, channels].
        """
    expanded_a = tf.expand_dims(flattened, 1)    # shape: (6, 1)
    expanded_b = tf.expand_dims(flattened, 0)    # shape: (1, 6)
    out3 = tf.einsum('i,j->ij', flattened, flattened)
    out3_ = tf.matmul(expanded_a, expanded_b)
    assert np.allclose(out3.eval(), out3_.eval())
    
    # Transpose
    out4 = tf.einsum('ij->ji', x)     # shape: (3, 2)
    out4_ = tf.transpose(x)
    assert np.allclose(out4.eval(), out4_.eval())
    
    
    # ------ Scan ------ #
    
    """
    Ex1. Compute the cumulative sum of x along axis 1.
    """
    x = tf.constant([[1,2,3], [4,5,6]])
    out = tf.cumsum(x, axis=1)       # tf.cumsum == np.cumsum
    print (out.eval())
    
    """
    Ex2. Compute the cumulative product of x along axis 1.
    """
    x = tf.constant([[1,2,3], [4,5,6]])
    out = tf.cumprod(x, axis=1)      # tf.cumprod == np.cumprod
    print (out.eval())
    
    
    # ------ Segmentation ------ #
    
    """
    Ex3. Compute the sum along the first two elements and the last two elements of x separately.
    """
    x = tf.constant([[1,2,3,4],
                      [-1,-2,-3,-4],
                      [-10,-20,-30,-40],
                      [10,20,30,40]])
    out = tf.segment_sum(x, [0, 0, 1, 1])
    print (out.eval())
    
    """
    Ex4. Compute the product along the first two elements and the last two elements of x separately.
    """
    x = tf.constant([[1,2,3,4],
                     [1,1/2,1/3,1/4],
                     [1,2,3,4],
                     [-1,-1,-1,-1]])
    out = tf.segment_prod(x, [0, 0, 1, 1])
    print (out.eval())
    
    """
    Ex5. Compute the minimum along the first two elements and the last two elements of x separately.
    """
    x = tf.constant([[1,4,5,7],
                     [2,3,6,8],
                     [1,2,3,4],
                     [-1,-2,-3,-4]])
    out = tf.segment_min(x, [0, 0, 1, 1])
    print (out.eval())
    
    """
    Ex6. Compute the mean along the first two elements and the last two elements of x separately.
    """
    x = tf.constant([[1,4,5,7],
                     [2,3,6,8],
                     [1,2,3,4],
                     [-1,-2,-3,-4]], dtype=tf.float32)
    out = tf.segment_mean(x, [0, 0, 1, 1])
    print (out.eval())
    
    """
    Ex7. Compute the sum along the second and fourth and the first and third elements of x separately in the order.
    """
    x = tf.constant([[1,4,5,7],
                     [2,3,6,8],
                     [1,2,3,4],
                     [-1,-2,-3,-4]])
    out = tf.unsorted_segment_sum(x, [1, 0, 1, 0], num_segments=2)
    print (out.eval())
    
    
    # ------ Sequence Comparison and Indexing ------ #
    
    """
    Ex8. Get the indices of maximum and minimum values of x along the second(row) axis.
    """
    _x = np.random.permutation(10).reshape(2, 5)
    print (_x)
    x = tf.convert_to_tensor(_x)
    out1 = tf.argmax(x, axis=1)    # Returns the index with the largest value across axes of a tensor.
    out2 = tf.argmin(x, axis=1)
    print (out1.eval())
    print (out2.eval())
    
    """
    Ex9. Find the unique elements of x that are not present in y.
    """
    x = tf.constant([1,2,3,4,5])
    y = tf.constant([0,1,2,3])
    out, idx = tf.setdiff1d(x, y)
    print (out.eval())
    print (idx.eval())
    
    """
    Ex10. Return the elements of x, if x < 4, otherwise x*10.
    """
    _x = np.arange(1, 10).reshape(3, 3)
    x = tf.convert_to_tensor(_x)
    out = tf.where(x < 4, x, x*10)     # # tf.where == np.where
    print (out.eval())
    
    """
    Ex11. Get unique elements and their indices from x.
    """
    _x = np.array([1, 2, 6, 4, 2, 3, 2])
    x = tf.convert_to_tensor(_x)
    out, indices = tf.unique(x)
    print (out.eval())
    print (indices.eval())
    # Note that tf.unique keeps the original order, whereas np.unique sorts the unique members.
    _out, _indices = np.unique(_x, return_inverse=True)
    print ("sorted unique elements =", _out)
    print ("indices =", _indices)
    
    """
    Ex12. Compute the edit distance between hypothesis and truth.
    """
    hypothesis = tf.SparseTensor([[0, 0],[0, 1],[0, 2],[0, 4]],
                                 ["a", "b", "c", "a"], (1, 5))
    # Note that this is equivalent to the dense tensor: [["a", "b", "c", 0, "a"]]
    
    truth = tf.SparseTensor([[0, 0],[0, 2],[0, 4]],
                            ["a", "c", "b"], (1, 6))
    # This is equivalent to the dense tensor: [["a", 0, "c", 0, "b", 0]]
    out1 = tf.edit_distance(hypothesis, truth, normalize=False)
    out2 = tf.edit_distance(hypothesis, truth, normalize=True)
    print (out1.eval())    # 2 <- one deletion ("b") and one substitution ("a" to "b")
    print (out2.eval())    # 0.6666 <- 2 / 6
    

    Part 4:Tensor Transformations

    import numpy as np
    import tensorflow as tf
    
    print (tf.__version__)      # 1.0.0
    
    sess = tf.InteractiveSession()
    
    """
    Ex1. Let x be a tensor of [["1.1", "2.2"], ["3.3", "4.4"]]. Convert the datatype of x to float32.
    """
    x = tf.constant([["1.1", "2.2"], ["3.3", "4.4"]])    # Defaults to tf.float32
    out = tf.string_to_number(x)
    print out.eval()
    
    """
    Ex2. Let x be a tensor [[1, 2], [3, 4]] of int32. Convert the data type of x to float64.
    """
    x = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)
    out1 = tf.to_double(x)      # Casts a tensor to type float64.
    out2 = tf.cast(x, tf.float64)
    assert np.allclose(out1.eval(), out2.eval())
    print out1.eval()
    
    """
    Ex3. Let x be a tensor [[1, 2], [3, 4]] of int32. Convert the data type of x to float32.
    """
    x = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)
    out1 = tf.to_float(x)      # Casts a tensor to type float32.
    out2 = tf.cast(x, tf.float32)
    assert np.allclose(out1.eval(), out2.eval())
    print out1.eval()
    
    """
    Ex4. Let x be a tensor [[1, 2], [3, 4]] of float32. Convert the data type of x to int32.
    """
    x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
    ou
    t1 = tf.to_int32(x)      # Casts a tensor to type int32.
    out2 = tf.cast(x, tf.int32)
    assert np.allclose(out1.eval(), out2.eval())
    print out1.eval()
    
    """
    Ex5. Let x be a tensor [[1, 2], [3, 4]] of float32. Convert the data type of x to int64.
    """
    x = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
    out1 = tf.to_int64(x)      # Casts a tensor to type int64. 
    out2 = tf.cast(x, tf.int64)
    assert np.allclose(out1.eval(), out2.eval())
    print out1.eval()
    assert np.allclose(out1.eval(), arr.astype(np.int64))
    
    """
    Ex6. Let x be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]. 
         Create the tensor representing the shape of x.
    """
    x = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
    out = tf.shape(x)
    print out.eval()
    
    """
    Ex7. Let x be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]) and y be a tensor [10, 20]. 
         Create a list of tensors representing the shape of X and Y.
    """
    x = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
    y = tf.constant([10, 20])
    out_x, out_y = tf.shape_n([x, y]) 
    print out_x.eval(), out_y.eval()
    
    """
    Ex8. Let x be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]. 
         Create a tensor representing the size (total number of elements) of x.
    """
    arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
    x = tf.constant(arr)
    out = tf.size(x)
    print out.eval()
    assert out.eval() == arr.size
    
    """
    Ex9. Let x be a tensor of [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]. 
         Create a tensor representing the rank (number of dimensions) of x.
    """
    arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]])
    x = tf.constant(arr)
    out = tf.rank(x)
    print out.eval()
    assert out.eval() == arr.ndim
    
    """
    Ex10. Let x be tf.ones([2, 2, 3]). Reshape x so that the size of the second dimension equals 6 (means the size is [2,6]).
    """
    x = tf.ones([2, 2, 3])
    out = tf.reshape(x, [-1, 6])
    print out.eval()
    
    """
    Ex11. Let x be tf.ones([10, 10, 1, 1]). Remove all the dimensions of size 1 in x.
    """
    x = tf.ones([10, 10, 1, 1])
    out = tf.squeeze(x)      # Removes dimensions of size 1 from the shape of a tensor.
    print out.eval().shape
    assert np.allclose(out.eval(), np.squeeze(np.ones([10, 10, 1, 1])))
    
    """
    Ex12. Let X be tf.ones([10, 10, 1, 1]). Remove the third and fourth dimensions in x.
    """
    x = tf.ones([10, 10, 1, 1])
    out = tf.squeeze(x, [2,3])
    print out.eval().shape
    
    """
    Ex13. Let x be a tensor:
          [[[1, 1, 1], [2, 2, 2]],
           [[3, 3, 3], [4, 4, 4]],
           [[5, 5, 5], [6, 6, 6]]].
          Extract the [[3, 3, 3], [4, 4, 4]] from x.
    """
    arr = np.array([[[1, 1, 1],
                     [2, 2, 2]],
                    [[3, 3, 3],
                     [4, 4, 4]],
                    [[5, 5, 5],
                     [6, 6, 6]]])
    x = tf.constant(arr)
    out = tf.slice(x, [1, 0, 0], [1, 2, 3])   # tf.slice(input_, begin, size, name=None)
    print out.eval()
    assert np.allclose(out.eval(), arr[1, :, :])
    
    """
    Ex14. Let x be a tensor of
          [[1 2]
           [3 4]
           [5 6]
           [7 8]
           [9 10]].
          Extract the [[1, 2], [5, 6], [9, 10]]] from x.
    """
    x = tf.reshape(tf.range(1, 11), [5, 2])
    out = tf.strided_slice(x, [0], [5], [2])    # tf.strided_slice(input_, begin, end, strides=None, ...)
    print out.eval()
    arr = np.reshape(np.arange(1, 11), [5, 2])
    assert np.allclose(out.eval(), arr[[0, 2, 4]])
    
    """
    Ex15. Let x be a tensor of
          [[1 2 3 4  5]
           [6 7 8 9 10]].
          Split x into 5 tensors along the second dimension.
    """
    x = tf.reshape(tf.range(1, 11), [2, 5])
    out = tf.split(x, num_or_size_splits=5, axis=1)
    print [each.eval() for each in out]
    
    """
    Ex16. Lex X be a tensor
          [[1 2 3]
           [4 5 6].
          Create a tensor looking like 
          [[1 2 3 1 2 3 1 2 3 ]
           [4 5 6 4 5 6 4 5 6 ]].
    """
    x = tf.reshape(tf.range(1, 7), [2, 3])
    out = tf.tile(x, [1, 3])
    print out.eval()
    
    """
    Ex17. Lex x be a tensor 
          [[1 2 3]
           [4 5 6].
          Pad 2 0's before the first dimension, 3 0's after the second dimension.
    """
    x = tf.reshape(tf.range(1, 7), [2, 3])
    out = tf.pad(x, [[2, 0], [0, 3]])   # paddings=[[2, 0], [0, 3]] indicates [[上,下],[左,右]]
    print out.eval()
    
    """
    Ex18. Lex x be a tensor 
          [[1 2 3]
           [4 5 6]]
          and y be a tensor
          [[7 8 9]
           [10 11 12]]
          Concatenate x and y so that a new tensor looks like 
          [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]].
    """
    x = tf.constant([[1, 2, 3], [4, 5, 6]])
    y = tf.constant([[7, 8, 9], [10, 11, 12]])
    out = tf.concat([x, y], axis=1)     # equal np.concatenate((x, y), axis=1))
    print out.eval()
    
    """
    Ex19. Let x be a tensor [1, 4], y be a tensor [2, 5], and z be a tensor [3, 6]. 
          Create a single tensor from these such that it looks [[1, 2, 3], [4, 5, 6]].
    """
    x = tf.constant([1, 4])
    y = tf.constant([2, 5])
    z = tf.constant([3, 6])
    out = tf.stack([x, y, z], axis=1)   # tf.stack([x, y, z], axis=0) ==> [[1, 4], [2, 5], [3, 6]]
    print out.eval()
    
    """
    Ex20. Let x be a tensor [[1, 2, 3], [4, 5, 6]]. Convert X into 3 tensors: 
          [1, 4], [2, 5], [3, 6].
    """
    x = tf.constant([[1, 2, 3], [4, 5, 6]])
    out = tf.unstack(x, axis=1)
    print [each.eval() for each in out]
    
    """
    Ex21. Let X = tf.resahpe(tf.range(1, 1*2*3*4+1), [1, 2, 3, 4]). Reverse the last dimension.
          [[[[ 1  2  3  4]                  [[[[ 4  3  2  1]
             [ 5  6  7  8]                     [ 8  7  6  5]
             [ 9 10 11 12]]                    [12 11 10  9]]
                                  ===>
            [[13 14 15 16]                    [[16 15 14 13]
             [17 18 19 20]                     [20 19 18 17]
             [21 22 23 24]]]]                  [24 23 22 21]]]]
    """
    X = tf.reshape(tf.range(1, 1*2*3*4+1), [1, 2, 3, 4])
    out = tf.reverse(X, axis=[3])
    print out.eval()
    
    """
    Ex22. Let x = tf.constant([[1, 2, 3], [4, 5, 6]]). Transpose x.
    """
    x = tf.constant([[1, 2, 3], [4, 5, 6]])
    out = tf.transpose(x)
    print out.eval()
    
    """
    Ex23. Let x be a tensor [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Get the first, and third rows.
    """
    x = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    out1 = tf.gather(x, [0, 2])
    out2 = tf.gather_nd(x, [[0], [2]])
    assert np.allclose(out1.eval(), out2.eval())
    print out1.eval()
    
    """
    Ex24. Let x be a tensor [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Get the 5 and 7.
    """
    x = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    out = tf.gather_nd(x, [[1, 1], [2, 0]])
    print out.eval()
    
    """
    Ex25. Let x be a tensor [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get the unique elements and their counts.
          e.g. # tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]
               y, idx, count = unique_with_counts(x)
               y ==> [1, 2, 4, 7, 8]
               idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
               count ==> [2, 1, 3, 1, 2]
    """
    x = tf.constant([2, 2, 1, 5, 4, 5, 1, 2, 3])
    out1, _, out2 = tf.unique_with_counts(x)
    print out1.eval(), out2.eval()
    
    

    相关文章

      网友评论

        本文标题:TensorFlow: Basic Exercises (更新中

        本文链接:https://www.haomeiwen.com/subject/vbwewttx.html