Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow: create tf.NodeDef() and set attributes

I'm trying to create a new node and set its attributes.

For example printing one of the graph nodes I see that its attributes are:

attr {
  key: "T"
  value {
    type: DT_FLOAT
  }
}

I can create a node like:

node = tf.NodeDef(name='MyConstTensor', op='Const',
                   attr={'value': tf.AttrValue(tensor=tensor_proto),
                         'dtype': tf.AttrValue(type=dt)})

but how to add key: "T" atribute? i.e. what should be inside tf.AttrValue in this case?

Looking at attr_value.proto I have tried:

node = tf.NodeDef()
node.name = 'MySub'
node.op = 'Sub'
node.input.extend(['MyConstTensor', 'conv2'])
node.attr["key"].s = 'T' # TypeError: 'T' has type str, but expected one of: bytes

UPDATE:

I figured out that in Tensorflow it should be written like:

node.attr["T"].type = b'float32'

But this gives an error:

TypeError: b'float32' has type bytes, but expected one of: int, long

And I'm not sure which int value corresponds to float32.

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/attr_value.proto#L23

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/attr_value.proto#L35

like image 421
mrgloom Avatar asked May 27 '19 12:05

mrgloom


Video Answer


2 Answers

By trial and error I fugire out that it's just:

node.attr["T"].type = 1 # to set type to float32
like image 157
mrgloom Avatar answered Sep 23 '22 10:09

mrgloom


Try passing T as a byte:

node.attr["key"].s = b'T'

If you would like to pass more characters try the bytearray class.

In the protobuf definition of AttrValue s is defined as bytes not string. The Protobuf manual states that this should be a string in python but your error suggests it is more like a byte array.

like image 42
Bart Avatar answered Sep 20 '22 10:09

Bart