c = tfg.Variable(orange_price, name="c")
d = tfg.Variable(orange_num, name="d")

e = tfg.Variable(tax, name="e")

# Create Mul operation node
f = tfg.Mul(a, b, name="f")
g = tfg.Mul(c, d, name="g")

# Create Add operation node
h = tfg.Add(f, g, name="h")

# Create Mul operation node
i = tfg.Mul(h, e, name="i")

session = tfs.Session()
# forward
final_price = session.run(i, verbose=False)
print("final_price: {:f}".format(float(final_price)))

print()

#backward
d_in = 1
d_total_price, d_tax = i.backward(d_in)
print("d_total_price: {:f}".format(float(d_total_price)))
print("d_tax: {:f}".format(float(d_tax)))

d_total_apple_price, d_total_orange_price = h.backward(d_total_price)
print("d_total_apple_price: {:f}".format(float(d_total_apple_price)))
print("d_total_orange_price: {:f}".format(float(d_total_orange_price)))
Beispiel #2
0
    def __init__(self,
                 input_dim,
                 cnn_param_list,
                 fc_hidden_size,
                 output_size,
                 input_node=None,
                 target_node=None,
                 conv_initializer=tfe.Initializer.Conv_Xavier_Normal,
                 initializer=tfe.Initializer.Normal.value,
                 init_sd=0.01,
                 activator=tfe.Activator.ReLU.value,
                 optimizer=tfe.Optimizer.SGD.value,
                 learning_rate=0.01):

        super().__init__()

        self.input_dim = input_dim
        self.cnn_param_list = cnn_param_list
        self.fc_hidden_size = fc_hidden_size
        self.output_size = output_size

        self.input_node = input_node
        self.target_node = target_node

        self.conv_initializer = conv_initializer
        self.initializer = initializer
        self.activator = activator
        self.optimizer = optimizer(learning_rate=learning_rate)

        self.params = {}
        self.optimal_epoch_and_params = None

        self.output = None
        self.error = None
        self.max_epoch = None

        self.session = tfs.Session()

        self.mode_id = ''.join(
            random.choices(string.ascii_uppercase + string.digits, k=8))

        print("Convolutional Neural Network Model - ID:", self.mode_id)

        self.params_size_list = None
        self.layers = OrderedDict()

        self.train_error_list = []
        self.validation_error_list = []
        self.test_accuracy_list = []

        self.min_validation_error_epoch = sys.maxsize
        self.min_train_error = sys.float_info.max
        self.min_validation_error = sys.float_info.max
        self.min_fold_idx = sys.maxsize
        self.test_accuracy_at_min_validation_error_epoch = 0.0

        self.min_validation_error_per_fold = []

        self.shape_before_fc = None
        self.num_neurons_flatten_for_fc = None

        self.initialize_param(sd=init_sd)
        self.layering()