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
By trial and error I fugire out that it's just:
node.attr["T"].type = 1 # to set type to float32
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With