Giter Site home page Giter Site logo

multiple compilers about fold HOT 13 CLOSED

fabioasdias avatar fabioasdias commented on May 18, 2024
multiple compilers

from fold.

Comments (13)

delesley avatar delesley commented on May 18, 2024 1

from fold.

mherreshoff avatar mherreshoff commented on May 18, 2024

from fold.

fabioasdias avatar fabioasdias commented on May 18, 2024

Marcello, I'm aware, however, as I said, when I construct a new compiler it doesn't work, with the error:

TypeError: block <td.Pipe> is already a child of <td.Pipe>

from fold.

mherreshoff avatar mherreshoff commented on May 18, 2024

from fold.

mherreshoff avatar mherreshoff commented on May 18, 2024

from fold.

fabioasdias avatar fabioasdias commented on May 18, 2024

So, I tried that approach. Here's the relevant (full) code:

CL=td.ScopedLayer(convLayer)
    
def mk_encod():
    return(td.Map(td.Tensor((24,10)) >>  CL >>
                  td.Function(lambda x: tf.reshape(x,[-1,64*(10-2)]))) >>
           td.Fold(td.Concat() >>
                   td.Function(td.FC(256)), td.FromTensor(tf.zeros(256))))

model = ( mk_encod()  >>  td.Function(td.FC(1)))
    
compiler = td.Compiler.create((model, td.Scalar()))
y, y_ = compiler.output_tensors
loss = tf.nn.l2_loss(y - y_)
train = tf.train.AdamOptimizer().minimize(loss)
sess.run(tf.global_variables_initializer())
for i in range(1000):
    sess.run(train, compiler.build_feed_dict([get_example() for _ in range(batch_size)]))

encod=mk_encod()
newComp=td.Compiler.create((encod,td.Scalar())) #copied the structure of the prev. compiler
fvBlock,_=newComp.output_tensors
FV=sess.run(fvBlock,newComp.build_feed_dict([get_real(i) for i in range(N)]))

However, that gets me:

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value FC_256_2/weights
	 [[Node: FC_256_2/weights/read = Identity[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](FC_256_2/weights)]]

So I guess I'm messing up with the scopes, I think...
Isn't this approach (train a whole net, use the results of a middle layer) somewhat common on NLP ?(isn't word2vec like this?). What would be the canonical way for that?

from fold.

delesley avatar delesley commented on May 18, 2024

from fold.

rudinger avatar rudinger commented on May 18, 2024

Is my understanding correct that there is (currently) no way to obtain/inspect the output_tensors of a non-root block without compiling a separate instance of the tf graph (for each such block), and then doing separate corresponding executions of sess.run(...), etc.? Since sess.run([tensor1, tensor2,...]) already allows arbitrary/non-root tensors in the tf graph to be specified, it seems like this may be an issue with the td compiler only being able to expose the root-level tensors in the resulting graph. Anyway, having a simple way to inspect non-root tensors would be very helpful for debugging purposes. Thanks for the great resource!

from fold.

delesley avatar delesley commented on May 18, 2024

from fold.

rudinger avatar rudinger commented on May 18, 2024

Ok, I think I see how one could post-hoc stitch a Metric into the pipeline to inspect values in a non-root block. A simple example of a graph that sums two numbers and doubles them, where we want to inspect the values at the intermediate summing stage:

import tensorflow as tf
sess = tf.InteractiveSession()
import tensorflow_fold as td

inputs = td.Record((td.Scalar(), td.Scalar()))
sum_block = inputs >> td.Function(tf.add)
m = td.Composition()
with m.scope():
    td.Metric('sum').reads(m.input)
    m.output.reads(m.input)
double_block = sum_block >> m >> td.Function(lambda x: 2.0*x)

compiler = td.Compiler.create(double_block)
fd = compiler.build_feed_dict([(1,2),(3,4),(5,6)])
outputs, metrics = sess.run([compiler.output_tensors, compiler.metric_tensors], feed_dict=fd)

I imagine there's a cleaner way to write this, but this at least accomplishes what I want (avoiding multiple compilers). Thanks for the tip!

from fold.

satwikkottur avatar satwikkottur commented on May 18, 2024

@rudinger Thanks for the stitch! Do you know if the retrieved outputs can be used for further ops in the tensorflow graph? Will the gradients flow back correctly if this is done?

from fold.

rudinger avatar rudinger commented on May 18, 2024

@satwikkottur Sorry I don't have an answer, I've moved to a different project. Good luck!

from fold.

satwikkottur avatar satwikkottur commented on May 18, 2024

I tried this by dropping down to loom API. But looks like I can specify the outputs (which are Loom id ints), and they stack up as output. Is there any other way I can get more structured outputs from loom, rather than rearranging them post loom?
For eg., I want [1, 2, 5] outputs together and [3, 4, 6] together. (note that integers here are loom ids)

If you drop down to the loom layer, you can expose any part of the
computation as an output.

from fold.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.