def create_inputs(vocab_dim):
    input_seq_axis = Axis('inputAxis')
    input_sequence = sequence.input_variable(shape=vocab_dim,
                                             sequence_axis=input_seq_axis)
    label_sequence = sequence.input_variable(shape=vocab_dim,
                                             sequence_axis=input_seq_axis)

    return input_sequence, label_sequence
Example #2
0
def create_inputs(hidden_dim, sv_dim, vocab_dim):
    input_seq_axis = Axis('inputAxis')
    input_sequence = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis)
    # state in model, including sv vector, h vector, c vector
    sv_pair = C.input_variable(shape=sv_dim)
    inputH = C.input_variable(shape=hidden_dim)
    inputC = C.input_variable(shape=hidden_dim)
    label_sequence = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis)
    
    return input_sequence, sv_pair, label_sequence, inputH, inputC
Example #3
0
    def load_model(self):
        if self.__model:
            raise Exception("Model already loaded")

        trained_frcnn_model = load_model(self.__model_path)

        # cache indices of the model arguments
        args_indices = {}
        for i, arg in enumerate(trained_frcnn_model.arguments):
            args_indices[arg.name] = i

        self.__nr_rois = trained_frcnn_model.arguments[
            args_indices["rois"]].shape[0]
        self.__resize_width = trained_frcnn_model.arguments[
            args_indices["features"]].shape[1]
        self.__resize_height = trained_frcnn_model.arguments[
            args_indices["features"]].shape[2]
        self.labels_count = trained_frcnn_model.arguments[
            args_indices["roiLabels"]].shape[1]

        # next, we adjust the clone the model and create input nodes just for the features (image) and ROIs
        # This will make sure that only the calculations that are needed for evaluating images are performed
        # during test time
        #
        # find the original features and rois input nodes
        features_node = find_by_name(trained_frcnn_model, "features")
        rois_node = find_by_name(trained_frcnn_model, "rois")

        #  find the output "z" node
        z_node = find_by_name(trained_frcnn_model, 'z')

        # define new input nodes for the features (image) and rois
        image_input = input_variable(features_node.shape, name='features')
        roi_input = input_variable(rois_node.shape, name='rois')

        # Clone the desired layers with fixed weights and place holder for the new input nodes
        cloned_nodes = combine([z_node.owner]).clone(
            CloneMethod.freeze, {
                features_node: placeholder(name='features'),
                rois_node: placeholder(name='rois')
            })

        # apply the cloned nodes to the input nodes to obtain the model for evaluation
        self.__model = cloned_nodes(image_input, roi_input)

        # cache the indices of the input nodes
        self.__args_indices = {}

        for i, arg in enumerate(self.__model.arguments):
            self.__args_indices[arg.name] = i
Example #4
0
def create_model_placeholders():
    '''
    Create placeholders for getting features and labels into the model
    :return: returns a feature placeholder and a label placeholder
    '''
    input_sequence = sequence.input_variable(shape=VOCAB_SIZE_VAR,
                                             name='input_sequence')
    label_sequence = input_variable(shape=NUM_OUTPUT_CLASSES,
                                    name='label_sequence')
    return input_sequence, label_sequence
Example #5
0
def create_inputs(vocab_dim):
    input_seq_axis = Axis('inputAxis')
    input_sequence = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis)
    label_sequence = sequence.input_variable(shape=vocab_dim, sequence_axis=input_seq_axis)
    
    return input_sequence, label_sequence
Example #6
0
minibatch_size = 100


def get_sample(p):
    xi = [char_to_ix[ch] for ch in data[p:p + minibatch_size]]
    yi = [char_to_ix[ch] for ch in data[p + 1:p + minibatch_size + 1]]

    X = np.eye(vocab_size, dtype=np.float32)[xi]
    Y = np.eye(vocab_size, dtype=np.float32)[yi]

    return [X], [Y]


get_sample(0)

input_sequence = sequence.input_variable(shape=vocab_size)
label_sequence = sequence.input_variable(shape=vocab_size)

model = Sequential([
    For(
        range(2), lambda: Sequential(
            [Stabilizer(),
             Recurrence(LSTM(256), go_backwards=False)])),
    Dense(vocab_size)
])

z = model(input_sequence)
z_sm = cntk.softmax(z)

ce = cross_entropy_with_softmax(z, label_sequence)
errs = classification_error(z, label_sequence)
Example #7
0

minibatch_size=100
def sample(p):
    xi = [char_to_ix[ch] for ch in data[p:p+minibatch_size]]
    yi = [char_to_ix[ch] for ch in data[p+1:p+minibatch_size+1]]
    
    X = np.eye(vocab_size, dtype=np.float32)[xi]
    Y = np.eye(vocab_size, dtype=np.float32)[yi]

    return [X], [Y]
sample(0)


input_seq_axis = Axis('inputAxis')
input_sequence = sequence.input_variable(shape=vocab_size, sequence_axis=input_seq_axis)
label_sequence = sequence.input_variable(shape=vocab_size, sequence_axis=input_seq_axis)

# model = Sequential([Dense(300),Dense(vocab_size)])

model = Sequential([
        For(range(2), lambda:
                   Sequential([Stabilizer(), Recurrence(LSTM(256), go_backwards=False)])),
        Dense(vocab_size)])

z = model(input_sequence)

ce = cross_entropy_with_softmax(z, label_sequence)
errs = classification_error(z, label_sequence)

lr_per_sample = learning_rate_schedule(0.001, UnitType.sample)
    for w,l in zip(words_test,labels_test):
        if dofill: w_a = to_onehot(fill(list(map(char_to_num,list(w)))))
        else: w_a = to_onehot(list(map(char_to_num,list(w))))
        out = net(w_a)[0]
        if (out[1]>0.5 and l==-1): correct+=1
        if (out[0]>0.5 and l==1): correct+=1
        total+=1
    print("{} out of {} correct ({}%)".format(correct,total,correct/total*100))

z_sm = C.softmax(z)
check(z_sm)

# Now implement simple RNN
words_arr1 = [to_onehot(list(map(char_to_num,list(w)))) for w in words]

input_var = sequence.input_variable(vocab_size)
label_var = C.input_variable(2)

model = Sequential([Recurrence(C.layers.RNNStep(200,activation=C.relu)),sequence.last,Dense(100,activation=C.relu),Dense(2)])

z = model(input_var)
z_sm = C.softmax(z)

ce = cross_entropy_with_softmax(z, label_var)
errs = classification_error(z, label_var)

lr_per_sample = learning_rate_schedule(0.02, UnitType.minibatch)
learner = C.learners.sgd(z.parameters, lr_per_sample)
progress_printer = ProgressPrinter(freq=100, tag='Training')
trainer = Trainer(z, (ce, errs), learner, progress_printer)