2

I am creating Custome Estimator API for a Time Series data. There occurs no error during TensorFlow graph Construction, but I get the mismatch shape error during the graph Computation. Before posting the code let me explain the little bit about the dataset I been using. The data I am using to create Custom Estimator is Time Series data which have two years history.

Dataset Shape is (13, 731)

My Goal is to predict the next 30 days store traffic.

I decided to use Convolutional Neural Network for High dimensional Time-series Forecasting. I took 701 data points for training and next 30 its corresponding label data points. I wrote following csv_input_fn to pass input data into Estimator.

def csv_input_fn(csv_path, batch_size):
    #input function
    def _input_fn():
        input_filename= tf.train.match_filenames_once(csv_path)

        filename_queue= tf.train.string_input_producer(
            input_filename, num_epochs=None, shuffle=True)

        reader = tf.TextLineReader()
        _, value =  reader.read_up_to(filename_queue, num_records= batch_size)
        value_column = tf.expand_dims(value, -1)

        all_data= tf.decode_csv(value_column, record_defaults=CSV_TYPES)
        inputs= all_data[:len(all_data) - pred_steps]
        labels= all_data[len(all_data) - pred_steps: ]

        inputs= tf.concat(inputs, axis=1)
        labels= tf.concat(labels, axis=1)

        return{'raw_data': inputs}, labels


    return _input_fn

where pred_steps=30 as I have to predict the last 30 days traffic. I have created my custom NN model which have 8 conv1d layers with dilation rates followed by dense, followed by a dropout, followed by another dense layer, and at last final layer. This the cnn_model_fn i created

def cnn_model_fn(features, labels, mode):

    #setup  the mode
    if mode == tf.estimator.ModeKeys.PREDICT:
        tf.logging.info("My Model Function: PREDICT, {}".format(mode))
    elif mode == tf.estimator.ModeKeys.EVAL:
        tf.logging.info("My Model Function: EVAL, {}".format(mode))
    elif mode ==  tf.estimator.ModeKeys.TRAIN:
        tf.logging.info("My Model Function: TRAIN, {}".format(mode))


    #set up the initializer
    #Input layer

    input_layer = tf.reshape(features['raw_data'], [-1, total_len_inp_features,1])

    #conv1 layer
    conv1 = tf.layers.conv1d(input_layer, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[0])

    #conv2 layer
    conv2 = tf.layers.conv1d(conv1, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[1])

    #conv3 layer
    conv3 = tf.layers.conv1d(conv2, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[2])

    #conv4 layer
    conv4 = tf.layers.conv1d(conv3, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[3])

    #conv5 layer
    conv5 = tf.layers.conv1d(conv4, filters=n_filters,
        kernel_size= filter_width,
        padding= 'same',
        dilation_rate= dilation_rates[4])

    #conv6 layer
    conv6 = tf.layers.conv1d(conv5, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[5])

    #conv7 layer
    conv7 = tf.layers.conv1d(conv6, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[6])

    #conv8 layer
    conv8 = tf.layers.conv1d(conv7, filters=n_filters,
        kernel_size= filter_width,
        padding='same',
        dilation_rate= dilation_rates[7])

    #add dense layer
    dense_1 = tf.layers.dense(conv8, units=128,
        activation= tf.nn.relu)

    #add dropout
    dropout= tf.layers.dropout(dense_1, rate=0.4)

    #add dense layer
    dense_2 = tf.layers.dense(dropout, units=1)

    #output layer
    outlen= tf.reshape(dense_2, [-1, len(OUTPUT_COLUMN_NAMES)])

    #predictions
    predictions= tf.layers.dense(outlen, 30, activation= None)

while running the model, I face no error during construction of the graph, but facing the following error during the computation of the graph.

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 9113 values, but the requested shape requires a multiple of 30 [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](dense_1/BiasAdd, Reshape_1/shape)]] [[Node: mean_squared_error/value/_227 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_1825_mean_squared_error/value", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

while Troubleshooting this problem I referred the link Invalid Argument Error. Shape Mismatch while computing gradients. I am sure, that my program is throwing error only because of Shape Mismatch. Since I am new to build Custom Estimators I have problems in figuring out the exact solutions.

For your reference, I am attaching the link Full code

Madhi
  • 1,206
  • 3
  • 16
  • 27
  • There definitely seems to be a difference between how a session is normally run vs using an Estimator that is afaik not in any of the Estimator documentation. – SantoshGupta7 Dec 03 '18 at 08:50

0 Answers0