Problem with defining softmax user function in cntk python api

56 Views Asked by At

I was following the cntk tutorial here https://github.com/microsoft/CNTK/tree/master/Examples/Speech/AN4/Python

I wanted to update to manipulate the SoftMax function, so that I can implement a certain paper and was using this cntk example as a playground Based on my search that the only way to manipulate layer outputs in cntk is through subclassing UserFunction and implementing whatever you want there in python as in the issue here https://github.com/Microsoft/CNTK/issues/2292

so I created my own SoftMax class as follows:

class MySoftmax(UserFunction):
def __init__(self, arg, name='MySoftmax'):
    super(MySoftmax, self).__init__([arg], name=name)

def forward(self, argument, device=None, outputs_to_retain=None):
    print((np.array(argument)))
    y = np.array(argument)
    print(y.shape)

    softmax_x = np.exp(argument) / np.sum(np.exp(argument), axis=0)
    print("softmax = ")
    print(softmax_x)
    return softmax_x,softmax_x

def backward(self, state, root_gradients):
    print("inback")
    softmax_x = state
    print(root_gradients)
    print("------")
    print(softmax_x)
    return root_gradients * softmax_x * (1 - softmax_x)

def infer_outputs(self):
    return [output_variable(self.inputs[0].shape, self.inputs[0].dtype,
        self.inputs[0].dynamic_axes)]

@staticmethod
def deserialize(inputs, name, state):
    print(inputs)
    return MySoftmax(inputs[0], name)

then I update the create_network_function as follows :

def create_recurrent_network():
# Input variables denoting the features and label data
features = sequence.input_variable(((2*context+1)*feature_dim))
labels = sequence.input_variable((num_classes))

# create network
model = Sequential([For(range(3), lambda : Recurrence(LSTM(256))),
                    Dense(num_classes)])
z = model(features)
# print("network output = ")
# print(type(z))
# y = MySoftmax(z)
# ce =y 
# print(type(cntk.ops.softmax(labels)))
print(user_function(MySoftmax(labels)))
g = user_function(MySoftmax(labels))
print(type(g))
print(cntk.ops.softmax(labels))
print(type(cntk.ops.softmax(labels)))




ce = cross_entropy_with_softmax(z, g)
errs = classification_error    (z, labels)

return {
    'feature': features,
    'label': labels,
    'ce' : ce,
    'errs' : errs,
    'output': z
}

but when I run I get these errors:

About to throw exception 'Cannot create an unpacked Value object from packed data where one or more sequences are truncated'

Traceback (most recent call last):
  File "custom_ts_train.py", line 273, in <module>
    cv_freq=args['cvfreq'])
  File "custom_ts_train.py", line 222, in htk_lstm_truncated
    train_and_test(network, trainer, train_source, test_source, minibatch_size, epoch_size, restore, model_path, cv_freq)
  File "custom_ts_train.py", line 189, in train_and_test
    model_inputs_to_streams = cv_input_map, frequency=cv_freq)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\cntk\internal\swig_helper.py", line 69, in wrapper
    result = f(*args, **kwds)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\cntk\train\training_session.py", line 333, in train
    super(TrainingSession, self).train(device)
  File "C:\Users\username\AppData\Local\Programs\Python\Python36\lib\site-packages\cntk\cntk_py.py", line 3599, in train
    return _cntk_py.TrainingSession_train(self, computeDevice)
RuntimeError: Cannot create an unpacked Value object from packed data where one or more sequences are truncated

    [CALL STACK]
    > CNTK::TrainingParameterSchedule<double>::  GetMinibatchSize
    - CNTK::Dictionary::  operator[]
    - CNTK::DictionaryValue::FreePtrAsType<CNTK::NDShape>
    - CNTK::Internal::  UseSparseGradientAggregationInDataParallelSGD
    - CNTK::TrainingParameterSchedule<double>::  GetMinibatchSize
    - CNTK::Internal::  UseSparseGradientAggregationInDataParallelSGD
    - CNTK::TrainingParameterSchedule<double>::  GetMinibatchSize
    - CNTK::Internal::  UseSparseGradientAggregationInDataParallelSGD
    - CNTK::Function::  Forward
    - CNTK::  CreateTrainer
    - CNTK::Trainer::  TotalNumberOfUnitsSeen
    - CNTK::Trainer::  TrainMinibatch (x2)
    - CNTK::TrainingSession::  Train
    - PyInit__cntk_py
    - PyCFunction_FastCallDict
0

There are 0 best solutions below