Created
November 26, 2018 14:11
-
-
Save noskill/c0a60d6ceded3619fa290a6b8129df37 to your computer and use it in GitHub Desktop.
pytorch in execution links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from torch import FloatTensor | |
| from torch.autograd import Variable | |
| from opencog.bindlink import execute_atom, evaluate_atom | |
| from opencog.scheme_wrapper import scheme_eval, scheme_eval_h | |
| from opencog.atomspace import TruthValue | |
| from opencog.backwardchainer import BackwardChainer | |
| from opencog.type_constructors import * | |
| from opencog.utilities import initialize_opencog | |
| from opencog.scheme_wrapper import load_scm | |
| import opencog.logger | |
| # Define the leaf nodes | |
| a = Variable(FloatTensor([4])) | |
| weights = [Variable(FloatTensor([i]), requires_grad=True) for i in (2, 5, 9, 7)] | |
| # unpack the weights for nicer assignment | |
| w1, w2, w3, w4 = weights | |
| # | |
| #b = w1 * a | |
| #c = w2 * a | |
| #d = w3 * b + w4 * c | |
| #L = (10 - d) | |
| # | |
| #L.backward() | |
| # | |
| global_storage = dict() | |
| global_storage[1] = lambda: a * w1 | |
| global_storage[2] = lambda: a * w2 | |
| global_storage[3] = lambda var1, var2: w3 * var1 + w4 * var2 | |
| atomspace = AtomSpace() | |
| initialize_opencog(atomspace) | |
| def runOp(num, a_concept): | |
| print("runOp" + str(num.name)) | |
| if not a_concept.name: | |
| return TruthValue(0.0, 0.0) | |
| print("a_concept:" + str(a_concept.name)) | |
| key = int(num.name) | |
| if key == 1: | |
| global_storage['result' + str(key)] = global_storage[key]() | |
| global_storage['result' + str(key)].retain_grad() | |
| print("intermediate result " + str(global_storage['result' + str(key)])) | |
| return a_concept | |
| if key == 2: | |
| global_storage['result' + str(key)] = global_storage[key]() | |
| global_storage['result' + str(key)].retain_grad() | |
| print("intermediate result " + str(global_storage['result' + str(key)])) | |
| return a_concept | |
| if key == 3: | |
| res1 = global_storage['result' + str(1)] | |
| res2 = global_storage['result' + str(2)] | |
| global_storage['result' + str(key)] = global_storage[key](res1, res2) | |
| global_storage['result' + str(key)].retain_grad() | |
| print('computation result: {0}'.format(global_storage['result' + str(key)])) | |
| L = (10 - global_storage['result' + str(key)]) | |
| L.backward() | |
| for index, weight in enumerate(weights, start=1): | |
| print(weight.grad) | |
| if(weight.grad): | |
| gradient, *_ = weight.grad.data | |
| print("Gradient of w{index} w.r.t to L: {gradient}".format(index=index, gradient=gradient)) | |
| return TruthValue(0.75,0.75) | |
| ConceptNode("Dummy") | |
| A = ExecutionOutputLink( | |
| GroundedSchemaNode("py:runOp"), | |
| ListLink(ConceptNode("1"), VariableNode("$Dummpy"))) | |
| B = ExecutionOutputLink( | |
| GroundedSchemaNode("py:runOp"), | |
| ListLink(ConceptNode("2"), | |
| A) | |
| ) | |
| C = EvaluationLink( | |
| GroundedPredicateNode("py:runOp"), | |
| ListLink(ConceptNode("3"), | |
| B)) | |
| evaluate_atom(atomspace, C) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment