def CAE_C_AE(input, input_labels, reuse=True):
    cae_e, _ = Encoder(input,
                       z_num=encoded_dimension,
                       repeat_num=cnn_layers,
                       hidden_num=node_growth_per_layer,
                       data_format=data_format,
                       reuse=True,
                       var_scope='CAE_Encoder')
    c_e, _ = Encoder(input,
                     z_num=encoded_dimension,
                     repeat_num=cnn_layers,
                     hidden_num=node_growth_per_layer,
                     data_format=data_format,
                     reuse=True,
                     var_scope='C_Encoder')
    ae_e, _ = Encoder(input,
                      z_num=encoded_dimension,
                      repeat_num=cnn_layers,
                      hidden_num=node_growth_per_layer,
                      data_format=data_format,
                      reuse=True,
                      var_scope='AE_Encoder')
    cae_lp, _ = ffnn(cae_e,
                     num_layers=ffnn_num_layers,
                     width=ffnn_width,
                     output_dim=n_labels,
                     activations=ffnn_activations,
                     reuse=True,
                     activate_last_layer=False,
                     var_scope='CAE_FFNN')
    c_lp, _ = ffnn(c_e,
                   num_layers=ffnn_num_layers,
                   width=ffnn_width,
                   output_dim=n_labels,
                   activations=ffnn_activations,
                   reuse=True,
                   activate_last_layer=False,
                   var_scope='C_FFNN')
    cae_aei, _ = Decoder(cae_e,
                         input_channel=image_channels,
                         repeat_num=cnn_layers,
                         hidden_num=node_growth_per_layer,
                         data_format=data_format,
                         reuse=True,
                         final_size=scale_size,
                         var_scope='CAE_Decoder')
    ae_aei, _ = Decoder(ae_e,
                        input_channel=image_channels,
                        repeat_num=cnn_layers,
                        hidden_num=node_growth_per_layer,
                        data_format=data_format,
                        reuse=True,
                        final_size=scale_size,
                        var_scope='AE_Decoder')
    cae_loss_ae = tf.losses.mean_squared_error(input, cae_aei)
    ae_loss_ae = tf.losses.mean_squared_error(input, ae_aei)
    cae_loss_ce = CrossEntropy(cae_lp, input_labels)
    c_loss_ce = CrossEntropy(c_lp, input_labels)
    return cae_aei, ae_aei, cae_lp, c_lp, cae_loss_ae, ae_loss_ae, cae_loss_ce, c_loss_ce
예제 #2
0
 def test_neuron_list(self):
     '''
     '''
     FFNN = ffnn.ffnn(0, [10, 20])
     FFNN._make_model(4, 1)
     self.assertEqual(len(FFNN.model),
                      len([(4, 10), 'relu', (10, 20), 'relu', (20, 1)]))
예제 #3
0
def oae_nonlinear_cnn(input,
                      lbls,
                      n_labels=None,
                      reuse=False,
                      encoded_dimension=16,
                      cnn_layers=4,
                      node_growth_per_layer=4,
                      data_format='NHWC',
                      image_channels=3,
                      var_scope='oae_linear_cnn'):
    with tf.variable_scope(var_scope):
        z, enc_vars = Encoder(input,
                              z_num=encoded_dimension,
                              repeat_num=cnn_layers,
                              hidden_num=node_growth_per_layer,
                              data_format=data_format,
                              reuse=reuse)
        z_o, offset_vars = ffnn(tf.concat([z, lbls], 1),
                                num_layers=3,
                                width=encoded_dimension,
                                output_dim=encoded_dimension,
                                activations=tf.elu,
                                activate_last_layer=False,
                                var_scope='offset',
                                reuse=reuse)
        output, dec_vars = Decoder(z_o,
                                   input_channel=image_channels,
                                   repeat_num=cnn_layers,
                                   hidden_num=node_growth_per_layer,
                                   data_format=data_format,
                                   reuse=reuse,
                                   final_size=scale_size)
        output = tf.maximum(tf.minimum(output, 1.), 0.)
    return z, z_o, output, enc_vars, dec_vars, offset_vars
예제 #4
0
 def test_not_conv(self):
     '''Test when model has not converged.
     '''
     FFNN = ffnn.ffnn(2, 10, n_iter_no_change=2, tol=1)
     self.assertTrue(FFNN._converge([2, 3]))
     self.assertTrue(FFNN._converge([4, 2, 2.5]))
     self.assertTrue(FFNN._converge([2, 0.5]))
예제 #5
0
 def test_list(self):
     '''Both neuron and f_act are lists
     '''
     FFNN = ffnn.ffnn(0, [20, 30], [nn.ReLU(), nn.ReLU(), nn.ReLU()])
     FFNN._make_model(4, 1)
     self.assertEqual(
         len(FFNN.model),
         len([(4, 20), 'relu', (20, 30), 'relu', (30, 1), 'relu']))
예제 #6
0
def myBackprop(x, t, W1, W2):

    y, z, a = ffnn(x, W1, W2)
    # y: Network output
    # z: hidden layer output
    # a: hidden layer input

    # Compute output Deltas
    deltak = outputDeltas(t, y)

    # Computing network Deltas
    deltaj = networkDeltas(deltak, a, W2)

    # Computing the
    dEn_dw1 = deltaj * np.insert(x, 0, 1)
    dEn_dw2 = deltak * np.insert(z, 0, 1)

    return [dEn_dw1, dEn_dw2, y]
예제 #7
0
def myBackprop(x,t,W1,W2):

	y,z,a = ffnn(x,W1,W2)
	# y: Network output
	# z: hidden layer output
	# a: hidden layer input

	# Compute output Deltas
	deltak = outputDeltas(t,y)

	# Computing network Deltas
	deltaj = networkDeltas(deltak,a,W2)

	# Computing the 
	dEn_dw1 = deltaj * np.insert(x,0,1)
	dEn_dw2 = deltak * np.insert(z,0,1)

	return [dEn_dw1, dEn_dw2, y]
예제 #8
0
def estimate(X, Y, X_CV, X_test, estimator, param, **kwargs):
    """ Handler method to distribute estimate to correct estimation method. """
    sys.path.insert(0, __path_to_source__)
    sys.path.insert(0, __path_to_elm__)
    assert 'estimator_id' in estimator, "estimate_handler: 'estimator_id' not in estimator"
    if estimator['estimator_id'] == 'knn':
        return knn(X, Y, X_CV, X_test, estimator, param, **kwargs)
    elif estimator['estimator_id'] == 'linreg':
        return linreg(X, Y, X_CV, X_test, estimator, param, **kwargs)
    elif estimator['estimator_id'] == 'sirknn':
        return sirknn(X, Y, X_CV, X_test, estimator, param, __path_to_source__,
                      **kwargs)
    elif estimator['estimator_id'] == 'ffnn':
        return ffnn(X, Y, X_CV, X_test, estimator, param, __path_to_source__,
                    **kwargs)
    elif estimator['estimator_id'] == 'isotron':
        return isotron(X, Y, X_CV, X_test, estimator, param,
                       __path_to_source__, **kwargs)
    elif estimator['estimator_id'] == 'elm':
        return elm_regressor(X, Y, X_CV, X_test, estimator, param,
                             __path_to_elm__, **kwargs)
    else:
        raise NotImplementedError("Estimator {0} is not implemented".format(
            estimator.get('estimator_id', 'NONE')))
예제 #9
0
def ff_pin(input,
           encoded_values,
           pins,
           width=[[5, 5, 0], [3, 3, 0], [4, 1, 0], [3, 3, 0], [5, 5, 0],
                  [0, 0, 2]],
           encoder_depth=3,
           num_layers=None,
           output_dim=None,
           activations=[tf.tanh, tf.nn.relu, ident],
           activate_last_layer=True,
           scope="FF_PIN",
           reuse=None):

    # Reading some implicit parameters
    if num_layers == None:
        if isinstance(width, list):
            num_layers = len(width)
        else:
            raise 'Need to provide num_layers or width as list'

    if reuse == None:
        local_scope = tf.get_variable_scope().name + '/' + scope
        scope_in_use = max([
            obj.name[:len(local_scope)] == local_scope
            for obj in tf.global_variables()
        ] + [False])
        reuse = scope_in_use
        if scope_in_use == True:
            warnings.warn('Re-using variables for ' + local_scope + ' scope')

    # Process the width and activation inputs into useable numbers
    if isinstance(width, list):
        if isinstance(width[0], list):
            layer_widths = width
        else:
            layer_widths = [width] * num_layers
    else:
        layer_widths = [[width] * len(activations)] * num_layers

    if output_dim == None:
        output_dim == sum(layer_widths[-1])

    # Check for coherency of final layer if needed
    if activate_last_layer and sum(layer_widths[num_layers - 1]) != output_dim:
        print layer_widths
        raise BaseException(
            'activate_last_layer == True but implied final layer width doesn\'t match output_dim \n (implied depth: '
            + str(sum(layer_widths[num_layers - 1])) + ', explicit depth: ' +
            str(output_dim) + ')')

    # The actual work
    with tf.variable_scope(scope, reuse=reuse):
        encoded_prediction = ffnn(input,
                                  num_layers=encoder_depth,
                                  width=layer_widths[:encoder_depth],
                                  output_dim=sum(layer_widths[encoder_depth -
                                                              1]),
                                  activations=activations,
                                  activate_last_layer=True,
                                  scope="Encoder",
                                  reuse=reuse)
        encoded_pinned = pins * encoded_values + (1 -
                                                  pins) * encoded_prediction
        output = ffnn(encoded_pinned,
                      num_layers=num_layers - encoder_depth,
                      width=layer_widths[encoder_depth:],
                      output_dim=output_dim,
                      activations=activations,
                      activate_last_layer=activate_last_layer,
                      scope="Decoder",
                      reuse=reuse)
    return output, encoded_prediction
예제 #10
0
#######################################################################
# model definition
#######################################################################

# The model for the real-data training samples
imgs, img_lbls, qr_f, qr_i = img_and_lbl_queue_setup(filenames, labels)
embeddings, enc_vars = Encoder(imgs,
                               z_num=encoded_dimension,
                               repeat_num=cnn_layers,
                               hidden_num=node_growth_per_layer,
                               data_format=data_format,
                               reuse=False)
label_predictions, ffnn_vars = ffnn(embeddings,
                                    num_layers=5,
                                    width=[[2 * n_labels]] * 4 + [[n_labels]],
                                    output_dim=n_labels,
                                    activations=[tf.tanh],
                                    activate_last_layer=False,
                                    scope="FFNN",
                                    reuse=False)
autoencoded_images, dec_vars = Decoder(embeddings,
                                       input_channel=image_channels,
                                       repeat_num=cnn_layers,
                                       hidden_num=node_growth_per_layer,
                                       data_format=data_format,
                                       reuse=False,
                                       final_size=scale_size)

# Run the model on a consistent selection of in-sample pictures
img_oos, lbls_oos, fs_oos = load_practice_images(oos_dir,
                                                 n_images=batch_size_x,
                                                 labels=labels)
예제 #11
0
# plt.show()

outputfile = open('Training optimization data.txt','w')

outputfile.write(str(W1i))
outputfile.write(str(W2i))
outputfile.write('Niter eta Training_MCR_C1 Training_MCR_C2 Test_MCR_C1 Test_MCR_2\n')
for Niter in [5,10,15,20,25,30,35]:
	for eta in [0.0001,0.0005,0.001,0.0015,0.002,0.0025,0.0030]:

		W1_trained, W2_trained = trainNN(X,T,W1i,W2i,Niter,eta)

		# Evaluating the training error
		y_training_class1=[]
		for exes in x1:
			y,z,a = ffnn(exes.T,W1_trained,W2_trained)
			y_training_class1.append(y[0,0])

		y_training_class2=[]
		for exes in x2:
			y,z,a = ffnn(exes.T,W1_trained,W2_trained)
			y_training_class2.append(y[0,0])

		MCR_1 = 0
		for value in y_training_class1:
			if value < 0.5:
				MCR_1 += 1

		MCR_2 = 0
		for value in y_training_class2:
			if value > 0.5:
예제 #12
0
def fader_cnn(images,
              true_labels=None,
              counter_labels=None,
              n_labels=None,
              reuse=False,
              encoded_dimension=16,
              cnn_layers=4,
              node_growth_per_layer=4,
              data_format='NHWC',
              image_channels=3,
              ffnn_layers=3,
              ffnn_width=n_labels,
              ffnn_activations=[tf.tanh]):
    embeddings, enc_vars = Encoder(images,
                                   z_num=encoded_dimension,
                                   repeat_num=cnn_layers,
                                   hidden_num=node_growth_per_layer,
                                   data_format=data_format,
                                   reuse=reuse)
    latent_embeddings = embeddings
    scores, ffnn_vars = ffnn(embeddings,
                             num_layers=ffnn_layers,
                             width=ffnn_width,
                             output_dim=n_labels,
                             activations=ffnn_activations,
                             activate_last_layer=False,
                             var_scope='Fader_FFNN',
                             reuse=reuse)
    estimated_labels = tf.tanh(scores)

    autoencoded, dec_vars = Decoder(tf.concat([embeddings, estimated_labels],
                                              1),
                                    input_channel=image_channels,
                                    repeat_num=cnn_layers,
                                    hidden_num=node_growth_per_layer,
                                    data_format=data_format,
                                    reuse=reuse,
                                    final_size=scale_size)
    autoencoded = tf.maximum(tf.minimum(autoencoded, 1.), 0.)
    reencoded_embeddings, _ = Encoder(autoencoded,
                                      z_num=encoded_dimension,
                                      repeat_num=cnn_layers,
                                      hidden_num=node_growth_per_layer,
                                      data_format=data_format,
                                      reuse=True)
    reencoded_latent_embeddings = reencoded_embeddings
    reencoded_scores, _ = ffnn(embeddings,
                               num_layers=ffnn_layers,
                               width=ffnn_width,
                               output_dim=n_labels,
                               activations=ffnn_activations,
                               activate_last_layer=False,
                               var_scope='Fader_FFNN',
                               reuse=True)
    reencoded_estimated_labels = tf.tanh(reencoded_scores)
    if true_labels is not None:
        fixed_labels = estimated_labels * (1 -
                                           tf.abs(true_labels)) + true_labels
    else:
        fixed_labels = estimated_labels
    output = {
        'latent_embeddings': latent_embeddings,
        'scores': scores,
        'estimated_labels': estimated_labels,
        'fixed_labels': fixed_labels,
        'autoencoded': autoencoded,
        'reencoded_scores': reencoded_scores,
        'reencoded_latent_embeddings': reencoded_latent_embeddings,
        'reencoded_estimated_labels': reencoded_estimated_labels,
        'enc_vars': enc_vars,
        'dec_vars': dec_vars,
        'ffnn_vars': ffnn_vars,
        'images': images,
        'losses': {
            'ae':
            tf.losses.mean_squared_error(images, autoencoded),
            'label':
            CrossEntropy(scores, true_labels)
            if true_labels is not None else tf.zeros_like(scores),
            're_embed':
            tf.losses.mean_squared_error(latent_embeddings,
                                         reencoded_latent_embeddings),
            're_label':
            CrossEntropy(reencoded_scores, fixed_labels)
        }
    }
    if true_labels is not None:
        output['true_labels'] = true_labels

    if counter_labels is None:
        counter_output = 'Nothing here'
    else:
        counter_fixed_labels = estimated_labels * (
            1 - tf.abs(counter_labels)) + counter_labels
        counter_autoencoded, _ = Decoder(tf.concat(
            [latent_embeddings, counter_fixed_labels], 1),
                                         input_channel=image_channels,
                                         repeat_num=cnn_layers,
                                         hidden_num=node_growth_per_layer,
                                         data_format=data_format,
                                         reuse=True,
                                         final_size=scale_size)
        counter_autoencoded = tf.minimum(tf.maximum(counter_autoencoded, 0.),
                                         1.)
        counter_reencoded_embeddings, _ = Encoder(
            counter_autoencoded,
            z_num=encoded_dimension,
            repeat_num=cnn_layers,
            hidden_num=node_growth_per_layer,
            data_format=data_format,
            reuse=True)
        counter_reencoded_latent_embeddings = counter_reencoded_embeddings
        counter_reencoded_scores, _ = ffnn(counter_reencoded_embeddings,
                                           num_layers=ffnn_layers,
                                           width=ffnn_width,
                                           output_dim=n_labels,
                                           activations=ffnn_activations,
                                           activate_last_layer=False,
                                           var_scope='Fader_FFNN',
                                           reuse=True)
        counter_reencoded_estimated_labels = tf.tanh(counter_reencoded_scores)
        output.update({
            'counter_labels':
            counter_labels,
            'counter_fixed_labels':
            counter_fixed_labels,
            'counter_autoencoded':
            counter_autoencoded,
            'counter_reencoded_scores':
            counter_reencoded_scores,
            'counter_reencoded_latent_embeddings':
            counter_reencoded_latent_embeddings,
            'counter_reencoded_estimated_labels':
            counter_reencoded_estimated_labels
        })
        output['losses'].update({
            'counter_similarity':
            tf.losses.mean_squared_error(autoencoded, counter_autoencoded),
            'counter_re_embed':
            tf.losses.mean_squared_error(latent_embeddings,
                                         counter_reencoded_latent_embeddings),
            'counter_re_label':
            CrossEntropy(counter_reencoded_scores, counter_fixed_labels)
        })
    return output
예제 #13
0

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--file',
        type=pathlib.Path,
        help='Include CSV file using --file arg'
    )
    parser.add_argument('--cnn', action='store_true')
    parser.add_argument('--rnn', action='store_true')
    parser.add_argument('--ae', action='store_true')
    parser.add_argument('--ffnn', action='store_true')
    args = parser.parse_args()

    if not args.file:
        sys.exit('Include dataset to run network using: --file <PATH>')

    x_train, y_train = translate.run(args.file)

    if args.ffnn:
        ffnn(x_train, y_train)
    elif args.rnn:
        rnn(x_train, y_train)
    elif args.cnn:
        cnn(x_train, y_train)
    elif args.ae:
        ae(x_train, y_train)
    else:
        sys.exit('Include network type: [--cnn, --rnn, --ae, --ffnn]')
예제 #14
0
def master_process(tick):
    pred, nif, orig, corr, main_op, main_corr, hist_plot, y_traina, five_day_md = lstm_nn.lstmnn(
        tick, 50)
    forcst, tenyears, diff, ind, acf_1, jnd, pacf_1, ind2, acf_2, jnd2, pacf_2, pred_adj, testval = arima_tsm.arima_tsm(
    )
    score_senti = senti.sentiment_analysis(tick)

    final_pred, prices_a, rsq = ffnn.ffnn(400, pred, nif, orig, corr, main_op,
                                          forcst)

    next_day_pred = five_day_md * score_senti + final_pred[-1][0]

    time_curr = (str(datetime.now()))
    time_stamp = time_curr[-6:]

    figure(figsize=(16, 7))

    plt.tight_layout()
    plt.subplots_adjust(left=0.1,
                        bottom=0.1,
                        right=0.92,
                        top=0.95,
                        wspace=0.2,
                        hspace=0.25)

    plt.subplot(2, 4, 1)
    plt.title('LSTM Loss MSE', fontsize=6)
    plt.plot(hist_plot, color='red', label='loss MSE')
    plt.xlabel('Epoch Number', fontsize=6)
    plt.ylabel('Loss', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 2)
    plt.title('NIFTY 100 6 years', fontsize=6)
    ty = tenyears.values
    plt.plot(ty,
             color='blue',
             label='NIFTY-100 shows upward trend and seasonality')
    plt.xlabel('Observation Points (DAYS)', fontsize=6)
    plt.ylabel('NIFTY-100', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 3)

    plt.bar(ind,
            acf_1,
            color='blue',
            label='We can observe steady decrease in AC')
    plt.title('ACF of Undifferenced Series', fontsize=6)
    plt.xlabel('Observation point', fontsize=6)
    plt.ylabel('Auto Covariance', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 4)
    plt.bar(jnd,
            pacf_1,
            color='blue',
            label='We can Observe Sudden drop in PAC')
    plt.title('PACF of Undifferenced Series', fontsize=6)
    plt.xlabel('Observation point', fontsize=6)
    plt.ylabel('Partial Auto Covariance', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 5)

    plt.bar(ind2, acf_2, color='green', label='AC values for d=1, p=2')
    plt.title('ACF plot for First Differencing', fontsize=6)
    plt.xlabel('Observation Points', fontsize=6)
    plt.ylabel('Auto Covariance Values', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 6)

    plt.bar(jnd2, pacf_2, color='green', label='PAC values for d=1, q=2')
    plt.title('PACF plot for First Differencing', fontsize=6)
    plt.xlabel('Observation Points', fontsize=6)
    plt.ylabel('Partial Auto Covariance Values', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 7)

    plt.plot(pred_adj, color='green', label='ARIMA(2,1,2) Forecast')
    plt.plot(testval, color='grey', label='NIFTY-100')
    plt.title('ARIMA(2,1,2) Forecast', fontsize=6)
    plt.xlabel('Observation points(Days)', fontsize=6)
    plt.ylabel('NIFTY-60', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.subplot(2, 4, 8)
    plt.plot(final_pred, color='green', label='predicted')
    plt.plot(prices_a, color='black', label='actual')
    plt.xlabel('Observation Points (DAYS)', fontsize=6)
    plt.ylabel('Value', fontsize=6)
    plt.legend(prop={'size': 6})
    plt.xticks(fontsize=6)
    plt.yticks(fontsize=6)

    plt.suptitle('Graphs ' + tick, fontsize=12)

    dir_path = os.path.dirname(os.path.realpath(__file__))
    save_path = dir_path + '\static\output' + time_stamp + '.png'
    img_name = 'output' + time_stamp + '.png'

    plt.savefig(save_path)
    plt.close()

    return next_day_pred, rsq, img_name
예제 #15
0
outputfile = open('Training optimization data.txt', 'w')

outputfile.write(str(W1i))
outputfile.write(str(W2i))
outputfile.write(
    'Niter eta Training_MCR_C1 Training_MCR_C2 Test_MCR_C1 Test_MCR_2\n')
for Niter in [5, 10, 15, 20, 25, 30, 35]:
    for eta in [0.0001, 0.0005, 0.001, 0.0015, 0.002, 0.0025, 0.0030]:

        W1_trained, W2_trained = trainNN(X, T, W1i, W2i, Niter, eta)

        # Evaluating the training error
        y_training_class1 = []
        for exes in x1:
            y, z, a = ffnn(exes.T, W1_trained, W2_trained)
            y_training_class1.append(y[0, 0])

        y_training_class2 = []
        for exes in x2:
            y, z, a = ffnn(exes.T, W1_trained, W2_trained)
            y_training_class2.append(y[0, 0])

        MCR_1 = 0
        for value in y_training_class1:
            if value < 0.5:
                MCR_1 += 1

        MCR_2 = 0
        for value in y_training_class2:
            if value > 0.5:
예제 #16
0
 def test_neuron_int(self):
     '''
     '''
     FFNN = ffnn.ffnn(2, 10)
     FFNN._make_model(4, 1)
     self.assertEqual(len(FFNN.model), len([(4, 10), 'relu', (10, 1)]))
예제 #17
0
 def test_conv(self):
     '''Test when model has converged.
     '''
     FFNN = ffnn.ffnn(2, 10, n_iter_no_change=2, tol=1)
     self.assertFalse(FFNN._converge([2, 2]))
     self.assertFalse(FFNN._converge([2, 1.5]))
예제 #18
0
    return np.matrix(array)


USAGE = """USAGE: tester.py "input pattern" "weight matrix 1" "weight matrix 2"
	Example: tester.py x W1 W2
	Where x is a Dx1 dimensional vector
	W1 is Dx(Mh+1) dimensional matrix
	W2 is (Mh+1)xMo dimensional matrix
"""

if len(sys.argv) != 4:
    #print(USAGE)
    exit(1)

script, x_input, W1_input, W2_input = sys.argv

x = np.matrix(np.loadtxt(x_input))
W1 = np.matrix(np.loadtxt(W1_input))
W2 = np.matrix(np.loadtxt(W2_input))

y, z, a = ffnn(x.T, W1, W2)

print("Network output: " + str(y) + '\n' + "Hidden layer input: " + str(a) +
      '\n' + "Hidden layer output: " + str(z))
print("This configuration has " + str(np.shape(W2)[1]) +
      " neurons in the hidden layer")

t = 1  # Synthesizing the target value so that we get some error when running the script :)

dEn_w1, dEn_w2, y = myBackprop(x.T, t, W1, W2)
예제 #19
0
 def test_len(self):
     '''Test if length is too short.
     '''
     FFNN = ffnn.ffnn(2, 10, n_iter_no_change=10)
     self.assertTrue(FFNN._converge([]))
     self.assertTrue(FFNN._converge([2, 3]))
예제 #20
0
	return np.matrix(array)


USAGE = """USAGE: tester.py "input pattern" "weight matrix 1" "weight matrix 2"
	Example: tester.py x W1 W2
	Where x is a Dx1 dimensional vector
	W1 is Dx(Mh+1) dimensional matrix
	W2 is (Mh+1)xMo dimensional matrix
"""


if len(sys.argv) != 4:
	#print(USAGE)
	exit(1)

script, x_input, W1_input, W2_input = sys.argv

x = np.matrix(np.loadtxt(x_input))
W1 = np.matrix(np.loadtxt(W1_input))
W2 = np.matrix(np.loadtxt(W2_input))

y,z,a = ffnn(x.T,W1,W2)

print("Network output: " + str(y) +'\n'+ "Hidden layer input: " + str(a) + '\n'+"Hidden layer output: " + str(z))
print("This configuration has " + str(np.shape(W2)[1]) + " neurons in the hidden layer")

t = 1	# Synthesizing the target value so that we get some error when running the script :)

dEn_w1, dEn_w2, y = myBackprop(x.T,t,W1,W2)

예제 #21
0
def master_process(tick):
	pred,nif,orig,corr, main_op, main_corr, hist_plot,y_traina = lstm_nn.lstmnn(tick,5)
	forcst, tenyears,diff, ind,acf_1,jnd,pacf_1, ind2,acf_2,jnd2,pacf_2,pred_adj,testval = arima_tsm.arima_tsm()
	score_senti = senti.sentiment_analysis(tick)

	final_pred, prices_a = ffnn.ffnn(40,pred,nif,orig,corr,main_op,forcst,score_senti)

	figure(figsize=(40,15))

	plt.subplot(2,4,1)
	plt.title('LSTM Loss MSE')
	plt.plot(hist_plot,color='red',label='loss MSE')
	plt.xlabel('Epoch Number')
	plt.ylabel('Loss')
	plt.legend()

	plt.subplot(2,4,2)
	plt.title('NIFTY 100 10 years')
	ty = tenyears.values
	plt.plot(ty,color='blue',label='NIFTY-100 shows upward trend and seasonality')
	plt.xlabel('Observation Points (DAYS)')
	plt.ylabel('NIFTY-100')
	plt.legend()


	plt.subplot(2,4,3)

	plt.bar(ind,acf_1,color='blue', label='We can observe steady decrease in AC')
	plt.title('ACF of Undifferenced Series')
	plt.xlabel('Observation point')
	plt.ylabel('Auto Covariance')
	plt.legend()


	plt.subplot(2,4,4)
	plt.bar(jnd,pacf_1,color='blue', label='We can Observe Sudden drop in PAC')
	plt.title('PACF of Undifferenced Series')
	plt.xlabel('Observation point')
	plt.ylabel('Partial Auto Covariance')
	plt.legend()

	plt.subplot(2,4,5)

	plt.bar(ind2,acf_2,color='green',label = 'AC values for d=1, p=2')
	plt.title('ACF plot for First Differencing')
	plt.xlabel('Observation Points')
	plt.ylabel('Auto Covariance Values')
	plt.legend()



	plt.subplot(2,4,6)


	plt.bar(jnd2,pacf_2,color='green',label = 'PAC values for d=1, q=2')
	plt.title('PACF plot for First Differencing')
	plt.xlabel('Observation Points')
	plt.ylabel('Partial Auto Covariance Values')
	plt.legend()

	plt.subplot(2,4,7)

	plt.plot(pred_adj, color='green', label='ARIMA(2,1,2) Forecast')
	plt.plot(testval,color='grey', label='NIFTY-100')
	plt.title('ARIMA(2,1,2) Forecast')
	plt.xlabel('Observation points(Days)')
	plt.ylabel('NIFTY-100')
	plt.legend()

	plt.subplot(2,4,8)
	plt.plot(final_pred, color='green',label='predicted')
	plt.plot(prices_a, color='black',label='actual')
	plt.xlabel('Observation Points (DAYS)')
	plt.ylabel('Value')
	plt.legend()

	plt.suptitle('Graphs', fontsize=20)

	plt.savefig('Output.png')
	plt.close()
#######################################################################
# starting the managed session routine
#######################################################################
tf.Graph().as_default()

#######################################################################
# model definition
#######################################################################

# The model for the real-data training samples
imgs, img_lbls, qr_f, qr_i = img_and_lbl_queue_setup(filenames, labels, batch_size=batch_size_x)
cae_embeddings, cae_enc_vars = Encoder(imgs, z_num=encoded_dimension, repeat_num=cnn_layers, hidden_num=node_growth_per_layer, data_format=data_format, reuse=False, var_scope='CAE_Encoder')
c_embeddings, c_enc_vars     = Encoder(imgs, z_num=encoded_dimension, repeat_num=cnn_layers, hidden_num=node_growth_per_layer, data_format=data_format, reuse=False, var_scope='C_Encoder')
ae_embeddings, ae_enc_vars   = Encoder(imgs, z_num=encoded_dimension, repeat_num=cnn_layers, hidden_num=node_growth_per_layer, data_format=data_format, reuse=False, var_scope='AE_Encoder')
cae_label_predictions, cae_ffnn_vars = ffnn(cae_embeddings, num_layers=5, width=[[2 * n_labels]] * 4 + [[n_labels]], output_dim=n_labels, activations=[tf.tanh], activate_last_layer=False, reuse=False, var_scope='CAE_FFNN')
c_label_predictions, c_ffnn_vars     = ffnn(  c_embeddings, num_layers=5, width=[[2 * n_labels]] * 4 + [[n_labels]], output_dim=n_labels, activations=[tf.tanh], activate_last_layer=False, reuse=False, var_scope='C_FFNN')
cae_autoencoded_images, cae_dec_vars = Decoder(cae_embeddings, input_channel=image_channels, repeat_num=cnn_layers, hidden_num=node_growth_per_layer, data_format=data_format, reuse=False, final_size=scale_size, var_scope='CAE_Decoder')
ae_autoencoded_images, ae_dec_vars   = Decoder( ae_embeddings, input_channel=image_channels, repeat_num=cnn_layers, hidden_num=node_growth_per_layer, data_format=data_format, reuse=False, final_size=scale_size, var_scope='AE_Decoder')

# Define the losses
lambda_ae = tf.placeholder(tf.float32, [])
cae_loss_ae = tf.losses.mean_squared_error(cae_autoencoded_images, imgs)
ae_loss_ae  = tf.losses.mean_squared_error( ae_autoencoded_images, imgs)
cae_loss_lbls_ce = CrossEntropy(cae_label_predictions, img_lbls)
c_loss_lbls_ce   = CrossEntropy(  c_label_predictions, img_lbls)
cae_loss_lbls_mse = tf.losses.mean_squared_error(img_lbls, tf.tanh(cae_label_predictions), weights=tf.abs(img_lbls))
c_loss_lbls_mse   = tf.losses.mean_squared_error(img_lbls, tf.tanh(  c_label_predictions), weights=tf.abs(img_lbls))
cae_loss_combined = cae_loss_lbls_ce + lambda_ae * cae_loss_ae
c_loss_combined   =   c_loss_lbls_ce
ae_loss_combined  =                    lambda_ae *  ae_loss_ae
예제 #23
0
# Read in training
train_df = pd.read_csv('train.csv')[:subset_train]
X = train_df.values
del train_df # free up some memory
I = np.identity(10)

# Strip the labels off of the training data
y = np.array([I[i] for i in X.T[0].T]) # one-hot the y's
X = X.T[1:].T 

# Give the data mean 0:
X = X.astype(float)
X -= 128.0

nn = ffnn(784,n_hidden,10,n_epochs,print_every,reg,alpha,minibatch=minibatch)
nn.fit(X,y)

# Compute the training error
if training_accuracy:
    nn.training_accuracy(X,y)


# Test the network
del X,y # free up some memory

X_test = pd.read_csv('test.csv').values
scores = []
for i in range(0,len(X_test),1000):
    # TODO this is a bugged line, it's outputting only zeros at the moment
    scores.extend([np.argmax(row) for row in nn.predict(X_test)])
                                   repeat_num=cnn_layers,
                                   hidden_num=node_growth_per_layer,
                                   data_format=data_format,
                                   reuse=False,
                                   var_scope='C_Encoder')
ae_embeddings, ae_enc_vars = Encoder(imgs,
                                     z_num=encoded_dimension,
                                     repeat_num=cnn_layers,
                                     hidden_num=node_growth_per_layer,
                                     data_format=data_format,
                                     reuse=False,
                                     var_scope='AE_Encoder')
cae_label_predictions, cae_ffnn_vars = ffnn(cae_embeddings,
                                            num_layers=ffnn_num_layers,
                                            width=ffnn_width,
                                            output_dim=n_labels,
                                            activations=ffnn_activations,
                                            activate_last_layer=False,
                                            reuse=False,
                                            var_scope='CAE_FFNN')
c_label_predictions, c_ffnn_vars = ffnn(c_embeddings,
                                        num_layers=ffnn_num_layers,
                                        width=ffnn_width,
                                        output_dim=n_labels,
                                        activations=ffnn_activations,
                                        activate_last_layer=False,
                                        reuse=False,
                                        var_scope='C_FFNN')
cae_autoencoded_images, cae_dec_vars = Decoder(
    cae_embeddings,
    input_channel=image_channels,
    repeat_num=cnn_layers,