Example #1
0
def get_filter_backprop_conv_results(max_val, input_shape, filter_shape,
                                     stride_shape, padding):
    """
    Compute depthwise filter backprop convolution.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use these to compute the convolution for the filter backprop pass.
    Returns the computed values in a numpy array.
    """
    total_inp_size = np.product(input_shape)
    input_vals = helpers.get_tensor_data(total_inp_size, max_val)

    inp_tensor = tf.constant(input_vals, shape=input_shape, dtype=np.float64)
    fil_tensor = tf.constant(0, shape=filter_shape, dtype=np.float64)
    output = tf.nn.depthwise_conv2d(inp_tensor,
                                    fil_tensor,
                                    strides=stride_shape,
                                    padding=padding,
                                    data_format="NHWC")

    output_shape = output.shape
    total_out_size = np.product(output_shape)
    output_vals = helpers.get_tensor_data(total_out_size, max_val)
    out_tensor = tf.constant(output_vals, shape=output_shape, dtype=np.float64)

    fil_size_tensor = tf.constant(filter_shape, shape=[len(filter_shape)])
    return tf.nn.depthwise_conv2d_backprop_filter(inp_tensor,
                                                  fil_size_tensor,
                                                  out_tensor,
                                                  strides=stride_shape,
                                                  padding=padding,
                                                  data_format="NHWC")
def get_matmul_result(max_val, batch, m, k, n, beta, trans_lhs, trans_rhs):
    """
    Construct and run a Tensorflow graph to compute matrix multiplication.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use these to compute the multiplication.

    Returns the computed values in a numpy array.
    """
    with tf.Graph().as_default():
        lhs_vals = helpers.get_tensor_data(batch * m * k, max_val)
        rhs_vals = helpers.get_tensor_data(batch * k * n, max_val)
        out_vals = helpers.get_tensor_data(batch * m * n, max_val)

        lhs_shape = get_shape(batch, m, k, trans_lhs)
        rhs_shape = get_shape(batch, k, n, trans_rhs)
        out_shape = get_shape(batch, m, n, False)

        lhs_tensor = tf.constant(lhs_vals, shape=lhs_shape, dtype=np.float64)
        rhs_tensor = tf.constant(rhs_vals, shape=rhs_shape, dtype=np.float64)
        initial_out = tf.constant(out_vals, shape=out_shape, dtype=np.float64)
        output = beta * initial_out + tf.matmul(lhs_tensor, rhs_tensor,
                                                trans_lhs, trans_rhs)

        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            sess.graph.finalize()
            return sess.run(output)
def get_forward_conv_results(max_val, input_shape, filter_shape, stride_shape,
                             padding):
    """
    Construct and run a Tensorflow graph to compute forward convolution.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use these to compute the convolution for the forward pass.
    Returns the computed values in a numpy array.
    """
    with tf.Graph().as_default():
        total_inp_size = np.product(input_shape)
        total_fil_size = np.product(filter_shape)

        input_vals = helpers.get_tensor_data(total_inp_size, max_val)
        filter_vals = helpers.get_tensor_data(total_fil_size, max_val)

        inp_tensor = tf.constant(input_vals,
                                 shape=input_shape,
                                 dtype=np.float64)
        fil_tensor = tf.constant(filter_vals,
                                 shape=filter_shape,
                                 dtype=np.float64)
        output = tf.nn.depthwise_conv2d_native(inp_tensor,
                                               fil_tensor,
                                               strides=stride_shape,
                                               padding=padding,
                                               data_format="NHWC")

        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            sess.graph.finalize()
            return sess.run(output)
Example #4
0
def get_pool_results(max_val, pool_op, input_shape, window_shape, stride_shape,
                     padding):
    """
    Construct and run a Tensorflow graph to compute pooling.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the pooling.
    Returns the computed values in a numpy array.
    """
    with tf.Graph().as_default():
        total_inp_size = np.product(input_shape)

        input_vals = helpers.get_tensor_data(total_inp_size, max_val)

        inp_tensor = tf.constant(input_vals,
                                 shape=input_shape,
                                 dtype=np.float64)
        output = tf.nn.pool(inp_tensor,
                            window_shape=window_shape,
                            pooling_type=TF_OPERATOR_MAP[pool_op],
                            strides=stride_shape,
                            padding=padding,
                            data_format="NHWC")

        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            sess.graph.finalize()
            return sess.run(output)
Example #5
0
def get_gradient_results(max_val, input_shape, is_training):
    """
    Compute gradient batchnorm.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the batchnorm op. Returns the computed values
    in a numpy array.
    """
    total_inp_size = np.product(input_shape)

    input_vals = helpers.get_tensor_data(total_inp_size, max_val)

    inp_tensor = tf.constant(input_vals, shape=input_shape, dtype=np.float64)

    mean = tf.math.reduce_mean(inp_tensor, axis=[0, 1, 2])

    variance = tf.math.reduce_variance(inp_tensor, axis=[0, 1, 2])

    output = tf.nn.batch_normalization(inp_tensor, mean, variance, 0., 1.,
                                       0.001)

    grad_x, grad_scale, grad_offset = compute_gradients(
        grad_y=np.array(input_vals, dtype=np.float64).reshape(input_shape),
        x=output,
        scale=np.ones(input_shape[3], dtype=np.float64),
        pop_mean=mean,
        pop_var=variance,
        epsilon=0.001,
        is_training=is_training)
    return grad_x, mean, variance, grad_scale, grad_offset
Example #6
0
def get_grad_results(max_val, pool_op, input_shape, window_shape, stride_shape,
                     padding):
    """
    Construct and run a Tensorflow graph to compute pooling backprop values.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the pooling, then create another tensor with
    the same values to use as the errors to back-propagate.
    Returns the computed values in a numpy array.
    """
    with tf.Graph().as_default():
        total_inp_size = np.product(input_shape)
        input_vals = helpers.get_tensor_data(total_inp_size, max_val)
        inp_tensor = tf.constant(input_vals,
                                 shape=input_shape,
                                 dtype=np.float64)

        pool_output = tf.nn.pool(inp_tensor,
                                 window_shape=window_shape,
                                 pooling_type=TF_OPERATOR_MAP[pool_op],
                                 strides=stride_shape,
                                 padding=padding,
                                 name='pool',
                                 data_format="NHWC")

        tf_op = tf.get_default_graph().get_operation_by_name('pool')
        grad_fn = get_gradient_function(tf_op)
        output_shape = pool_output.shape

        total_out_size = np.product(output_shape)
        error_vals = helpers.get_tensor_data(total_out_size, max_val)
        error_tensor = tf.constant(error_vals,
                                   shape=output_shape,
                                   dtype=np.float64)

        output = grad_fn(tf_op, error_tensor)

        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            sess.graph.finalize()
            return sess.run(output)
Example #7
0
def get_forward_conv_results(max_val, input_shape, filter_shape, stride_shape,
                             padding):
    """
    Compute forward convolution.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use these to compute the convolution for the forward pass.
    Returns the computed values in a numpy array.
    """
    total_inp_size = np.product(input_shape)
    total_fil_size = np.product(filter_shape)

    input_vals = helpers.get_tensor_data(total_inp_size, max_val)
    filter_vals = helpers.get_tensor_data(total_fil_size, max_val)

    inp_tensor = tf.constant(input_vals, shape=input_shape, dtype=np.float64)
    fil_tensor = tf.constant(filter_vals, shape=filter_shape, dtype=np.float64)
    return tf.nn.conv2d(inp_tensor,
                        fil_tensor,
                        strides=stride_shape,
                        padding=padding,
                        data_format="NHWC")
def get_matmul_result(max_val, batch, m, k, n, beta, trans_lhs, trans_rhs):
    """
    Compute matrix multiplication.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use these to compute the multiplication.

    Returns the computed values in a numpy array.
    """
    lhs_vals = helpers.get_tensor_data(batch * m * k, max_val)
    rhs_vals = helpers.get_tensor_data(batch * k * n, max_val)
    out_vals = helpers.get_tensor_data(batch * m * n, max_val)

    lhs_shape = get_shape(batch, m, k, trans_lhs)
    rhs_shape = get_shape(batch, k, n, trans_rhs)
    out_shape = get_shape(batch, m, n, False)

    lhs_tensor = tf.constant(lhs_vals, shape=lhs_shape, dtype=np.float64)
    rhs_tensor = tf.constant(rhs_vals, shape=rhs_shape, dtype=np.float64)
    initial_out = tf.constant(out_vals, shape=out_shape, dtype=np.float64)
    return beta * initial_out + tf.matmul(lhs_tensor, rhs_tensor, trans_lhs,
                                          trans_rhs)
Example #9
0
def get_bias_results(max_val, input_shape):
    """
    Compute bias-add.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the bias-add.
    Returns the computed values in a numpy array.
    """
    total_inp_size = np.product(input_shape)

    input_vals = helpers.get_tensor_data(total_inp_size, max_val)
    bias_vals = helpers.get_tensor_data(input_shape[3], max_val)

    inp_tensor = tf.constant(input_vals,
                             shape=input_shape,
                             dtype=np.float64)
    bias_tensor = tf.constant(bias_vals,
                              shape=(input_shape[3],),
                              dtype=np.float64)
    return tf.nn.bias_add(inp_tensor,
                          bias_tensor,
                          data_format="NHWC")
Example #10
0
def get_transpose_result(max_val, in_shape, permutation):
    """
    Compute transpose.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use this as the input to tf.transpose along with the given permutation.

    Returns the computed values in a numpy array.
    """
    total_size = np.prod(in_shape)
    in_vals = helpers.get_tensor_data(total_size, max_val)

    in_tensor = tf.constant(in_vals, shape=in_shape, dtype=np.float64)
    return tf.transpose(in_tensor, permutation)
Example #11
0
def get_forward_results(max_val, softmax_op, input_shape):
    """
    Compute forward softmax.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the softmax op. Returns the computed values
    in a numpy array.
    """
    total_inp_size = np.product(input_shape)

    input_vals = helpers.get_tensor_data(total_inp_size, max_val)

    inp_tensor = tf.constant(input_vals, shape=input_shape, dtype=np.float64)

    return softmax_op(inp_tensor)
Example #12
0
def get_pool_results(max_val, pool_op, input_shape, window_shape, stride_shape,
                     padding):
    """
    Compute forward pooling.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the pooling.
    Returns the computed values in a numpy array.
    """
    total_inp_size = np.product(input_shape)

    input_vals = helpers.get_tensor_data(total_inp_size, max_val)

    inp_tensor = tf.constant(input_vals, shape=input_shape, dtype=np.float64)
    return tf.nn.pool(inp_tensor,
                      window_shape=window_shape,
                      pooling_type=TF_OPERATOR_MAP[pool_op],
                      strides=stride_shape,
                      padding=padding,
                      data_format="NHWC")
Example #13
0
def get_transpose_result(max_val, in_shape, permutation):
    """
    Construct and run a Tensorflow graph to compute transpose.

    Will create input tensors of the required size filled with values 1, 2,
    3... and use this as the input to tf.transpose along with the given permutation.

    Returns the computed values in a numpy array.
    """
    with tf.Graph().as_default():
        total_size = np.prod(in_shape)
        in_vals = helpers.get_tensor_data(total_size, max_val)

        in_tensor = tf.constant(in_vals, shape=in_shape, dtype=np.float64)
        out_tensor = tf.transpose(in_tensor, permutation)

        with tf.Session() as sess:
            init = tf.global_variables_initializer()
            sess.run(init)
            sess.graph.finalize()
            return sess.run(out_tensor)
Example #14
0
def get_forward_results(max_val, input_shape):
    """
    Compute forward batchnorm.

    Will create an input tensor of the required size filled with values 1, 2,
    3... and use these to compute the batchnorm op. Returns the computed values
    in a numpy array.
    """
    total_inp_size = np.product(input_shape)

    input_vals = helpers.get_tensor_data(total_inp_size, max_val)

    inp_tensor = tf.constant(input_vals, shape=input_shape, dtype=np.float64)

    mean = tf.math.reduce_mean(inp_tensor, axis=[0, 1, 2])

    variance = tf.math.reduce_variance(inp_tensor, axis=[0, 1, 2])

    output = tf.nn.batch_normalization(inp_tensor, mean, variance, 0., 1.,
                                       0.001)

    return output, mean, variance
Example #15
0
        return decoded_words


def random_evaluate(evaluation_data, n=10):
    for i in range(n):
        pair = choice(evaluation_data)
        print('Instruction step', idx_to_words(pair[0], idx2word))
        print('Next step', idx_to_words(pair[1], idx2word))
        output_words = evaluate(encoder, decoder, pair[0].to(device))
        output_sentence = ' '.join(output_words)
        print('Generated instructions', output_sentence)
        print('')


# Load data
recipe_step_pairs, idx2word, word2idx, MAX_LENGTH = helpers.get_tensor_data()
n_words = len(word2idx)

#--- hyperparameters ---
N_EPOCHS = 1
LEARNING_RATE = 0.01
REPORT_EVERY = 50
HIDDEN_DIM = 256
#BATCH_SIZE = 20
#N_LAYERS = 1
teacher_forcing_ratio = 1
#TRAIN_SET_SIZE = int(len(recipe_step_pairs)*0.9)
TRAIN_SET_SIZE = 1000

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.set_num_threads(10)