练习

模拟投掷两个骰子 10 次。

创建一个骰子模拟,在模拟中生成一个 10x3 二维张量,其中:

        列 1 和 2 均存储一个骰子的一次投掷值。

        列 3 存储同一行中列 1 和 2 的值的总和。


with tf.Graph().as_default(), tf.Session() as sess:

  # Task 2: Simulate 10 throws of two dice. Store the results

  # in a 10x3 matrix.

  # We're going to place dice throws inside two separate

  # 10x1 matrices. We could have placed dice throws inside

  # a single 10x2 matrix, but adding different columns of

  # the same matrix is tricky. We also could have placed

  # dice throws inside two 1-D tensors (vectors); doing so

  # would require transposing the result.

    # 返回10*1的矩阵,产生于1和7之间,产生的值是均匀分布的

      dice1 = tf.Variable(tf.random_uniform([10, 1],minval=1, maxval=7,dtype=tf.int32))

      dice2 = tf.Variable(tf.random_uniform([10, 1],minval=1, maxval=7,dtype=tf.int32))

      # We may add dice1 and dice2 since they share the same shape

      # and size.

      dice_sum = tf.add(dice1, dice2)

      # We've got three separate 10x1 matrices. To produce a single

      # 10x3 matrix, we'll concatenate them along dimension 1.

    # 矩阵连接

    resulting_matrix = tf.concat(values=[dice1, dice2, dice_sum], axis=1)

      # The variables haven't been initialized within the graph yet,

      # so let's remedy that.

      sess.run(tf.global_variables_initializer())

      print(resulting_matrix.eval())


你可能感兴趣的:(练习)