Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

input must be a scalar, but it has shape [1] #301

Closed
zhengdi1992 opened this issue May 16, 2019 · 7 comments
Closed

input must be a scalar, but it has shape [1] #301

zhengdi1992 opened this issue May 16, 2019 · 7 comments

Comments

@zhengdi1992
Copy link

in tensorflow javacpp..
the placeholder was define by

self.drop = tf.placeholder(tf.float32,name='drop')
self.phase = tf.placeholder(tf.bool,name='x_phase')

in java, how to build the tensor?
i have try like this

tensorflow.Tensor drop = new tensorflow.Tensor(tensorflow.DT_FLOAT, new TensorShape(new long[]{1}));
FloatRawIndexer dropIndexer = drop.createIndexer();
dropIndexer.put(0, 0.5f);

but occur the error when session run..

input must be a scalar, but it has shape [1]
	 [[Node: fc_1layer/cond/sub/Switch = Switch[T=DT_FLOAT, _class=["loc:@drop"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_drop_0_1, _arg_x_phase_0_0)]]

i think was the new TensorShape(new long[]{1}) should not like that, but a scalar shape?
but how build it? if use new TensorShape(), the data cannot fill into the tensor..dropIndexer.put(0, 0.5f); will occur ArrayIndexOutOfBoundsException exeption

@saudet
Copy link
Member

saudet commented May 16, 2019

TensorFlow considers arrays that have a number of dimensions equal to 0 as scalars, so something like new TensorShape(new long[]{}) works for that.

@zhengdi1992
Copy link
Author

but if that new TensorShape(new long[]{}), how can assign the value to the tensor?
if in python, can use like that and works well.. how to translate to javacpp?

drop = sess.graph.get_tensor_by_name('drop:0')
op = sess.graph.get_tensor_by_name('str_output:0')
sess.run(op, {drop: 0.5})

i got the ArrayIndexOutOfBoundsException exception, by use that..when drop.createIndexer();

tensorflow.Tensor drop = new tensorflow.Tensor(tensorflow.DT_FLOAT,
            new TensorShape(new long[]{}));
FloatRawIndexer dropIndexer = drop.createIndexer();
dropIndexer.put(0, 0.5f);

@saudet
Copy link
Member

saudet commented May 16, 2019

There was an issue previously with scalars like that, but it should work with the latest release.

@zhengdi1992
Copy link
Author

the version is that?

<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>tensorflow</artifactId>
    <version>1.12.0-1.4.4</version>
</dependency>

@saudet
Copy link
Member

saudet commented May 16, 2019

No, 1.13.1-1.5, see: http://bytedeco.org/download/
The groupId and package names changed a bit:
http://bytedeco.org/news/2019/04/11/beyond-java-and-cpp/

@zhengdi1992
Copy link
Author

upgrade it and works well..
yep, changed a bit...hahaha..

saudet added a commit to bytedeco/javacpp-presets that referenced this issue May 16, 2019
@saudet
Copy link
Member

saudet commented May 16, 2019

I've added a couple more overloads for Tensor.create() so we can now do something like this instead:

        Tensor scalar = Tensor.create(new float[] {42});
        Tensor vector = Tensor.create(new float[] {0, 1}, 2);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants