1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 package org.tensorflow; 17 18 import static java.nio.charset.StandardCharsets.UTF_8; 19 20 /** Type-safe factory methods for creating {@link org.tensorflow.Tensor} objects. */ 21 public final class Tensors { 22 private Tensors() {} 23 24 /** 25 * Creates a scalar String tensor using the default, UTF-8 encoding. 26 * 27 * @param data The string to put into the new scalar tensor. 28 */ 29 public static Tensor<String> create(String data) { 30 return Tensor.create(data.getBytes(UTF_8), String.class); 31 } 32 33 /** 34 * Creates a scalar String tensor using a specified encoding. 35 * 36 * @param charset The encoding from String to bytes. 37 * @param data The string to put into the new scalar tensor. 38 */ 39 public static Tensor<String> create(String data, java.nio.charset.Charset charset) { 40 return Tensor.create(data.getBytes(charset), String.class); 41 } 42 43 /** 44 * Creates a scalar tensor containing a single {@code float} element. 45 * 46 * @param data The value to put into the new scalar tensor. 47 */ 48 public static Tensor<Float> create(float data) { 49 return Tensor.create(data, Float.class); 50 } 51 52 /** 53 * Creates a rank-1 tensor of {@code float} elements. 54 * 55 * @param data An array containing the values to put into the new tensor. The dimensions of the 56 * new tensor will match those of the array. 57 */ 58 public static Tensor<Float> create(float[] data) { 59 return Tensor.create(data, Float.class); 60 } 61 62 /** 63 * Creates a rank-2 tensor of {@code float} elements. 64 * 65 * @param data An array containing the values to put into the new tensor. The dimensions of the 66 * new tensor will match those of the array. 67 */ 68 public static Tensor<Float> create(float[][] data) { 69 return Tensor.create(data, Float.class); 70 } 71 72 /** 73 * Creates a rank-3 tensor of {@code float} elements. 74 * 75 * @param data An array containing the values to put into the new tensor. The dimensions of the 76 * new tensor will match those of the array. 77 */ 78 public static Tensor<Float> create(float[][][] data) { 79 return Tensor.create(data, Float.class); 80 } 81 82 /** 83 * Creates a rank-4 tensor of {@code float} elements. 84 * 85 * @param data An array containing the values to put into the new tensor. The dimensions of the 86 * new tensor will match those of the array. 87 */ 88 public static Tensor<Float> create(float[][][][] data) { 89 return Tensor.create(data, Float.class); 90 } 91 92 /** 93 * Creates a rank-5 tensor of {@code float} elements. 94 * 95 * @param data An array containing the values to put into the new tensor. The dimensions of the 96 * new tensor will match those of the array. 97 */ 98 public static Tensor<Float> create(float[][][][][] data) { 99 return Tensor.create(data, Float.class); 100 } 101 102 /** 103 * Creates a rank-6 tensor of {@code float} elements. 104 * 105 * @param data An array containing the values to put into the new tensor. The dimensions of the 106 * new tensor will match those of the array. 107 */ 108 public static Tensor<Float> create(float[][][][][][] data) { 109 return Tensor.create(data, Float.class); 110 } 111 112 /** 113 * Creates a scalar tensor containing a single {@code double} element. 114 * 115 * @param data The value to put into the new scalar tensor. 116 */ 117 public static Tensor<Double> create(double data) { 118 return Tensor.create(data, Double.class); 119 } 120 121 /** 122 * Creates a rank-1 tensor of {@code double} elements. 123 * 124 * @param data An array containing the values to put into the new tensor. The dimensions of the 125 * new tensor will match those of the array. 126 */ 127 public static Tensor<Double> create(double[] data) { 128 return Tensor.create(data, Double.class); 129 } 130 131 /** 132 * Creates a rank-2 tensor of {@code double} elements. 133 * 134 * @param data An array containing the values to put into the new tensor. The dimensions of the 135 * new tensor will match those of the array. 136 */ 137 public static Tensor<Double> create(double[][] data) { 138 return Tensor.create(data, Double.class); 139 } 140 141 /** 142 * Creates a rank-3 tensor of {@code double} elements. 143 * 144 * @param data An array containing the values to put into the new tensor. The dimensions of the 145 * new tensor will match those of the array. 146 */ 147 public static Tensor<Double> create(double[][][] data) { 148 return Tensor.create(data, Double.class); 149 } 150 151 /** 152 * Creates a rank-4 tensor of {@code double} elements. 153 * 154 * @param data An array containing the values to put into the new tensor. The dimensions of the 155 * new tensor will match those of the array. 156 */ 157 public static Tensor<Double> create(double[][][][] data) { 158 return Tensor.create(data, Double.class); 159 } 160 161 /** 162 * Creates a rank-5 tensor of {@code double} elements. 163 * 164 * @param data An array containing the values to put into the new tensor. The dimensions of the 165 * new tensor will match those of the array. 166 */ 167 public static Tensor<Double> create(double[][][][][] data) { 168 return Tensor.create(data, Double.class); 169 } 170 171 /** 172 * Creates a rank-6 tensor of {@code double} elements. 173 * 174 * @param data An array containing the values to put into the new tensor. The dimensions of the 175 * new tensor will match those of the array. 176 */ 177 public static Tensor<Double> create(double[][][][][][] data) { 178 return Tensor.create(data, Double.class); 179 } 180 181 /** 182 * Creates a scalar tensor containing a single {@code int} element. 183 * 184 * @param data The value to put into the new scalar tensor. 185 */ 186 public static Tensor<Integer> create(int data) { 187 return Tensor.create(data, Integer.class); 188 } 189 190 /** 191 * Creates a rank-1 tensor of {@code int} elements. 192 * 193 * @param data An array containing the values to put into the new tensor. The dimensions of the 194 * new tensor will match those of the array. 195 */ 196 public static Tensor<Integer> create(int[] data) { 197 return Tensor.create(data, Integer.class); 198 } 199 200 /** 201 * Creates a rank-2 tensor of {@code int} elements. 202 * 203 * @param data An array containing the values to put into the new tensor. The dimensions of the 204 * new tensor will match those of the array. 205 */ 206 public static Tensor<Integer> create(int[][] data) { 207 return Tensor.create(data, Integer.class); 208 } 209 210 /** 211 * Creates a rank-3 tensor of {@code int} elements. 212 * 213 * @param data An array containing the values to put into the new tensor. The dimensions of the 214 * new tensor will match those of the array. 215 */ 216 public static Tensor<Integer> create(int[][][] data) { 217 return Tensor.create(data, Integer.class); 218 } 219 220 /** 221 * Creates a rank-4 tensor of {@code int} elements. 222 * 223 * @param data An array containing the values to put into the new tensor. The dimensions of the 224 * new tensor will match those of the array. 225 */ 226 public static Tensor<Integer> create(int[][][][] data) { 227 return Tensor.create(data, Integer.class); 228 } 229 230 /** 231 * Creates a rank-5 tensor of {@code int} elements. 232 * 233 * @param data An array containing the values to put into the new tensor. The dimensions of the 234 * new tensor will match those of the array. 235 */ 236 public static Tensor<Integer> create(int[][][][][] data) { 237 return Tensor.create(data, Integer.class); 238 } 239 240 /** 241 * Creates a rank-6 tensor of {@code int} elements. 242 * 243 * @param data An array containing the values to put into the new tensor. The dimensions of the 244 * new tensor will match those of the array. 245 */ 246 public static Tensor<Integer> create(int[][][][][][] data) { 247 return Tensor.create(data, Integer.class); 248 } 249 250 /** 251 * Creates a scalar tensor containing a single {@code byte} element. 252 * 253 * @param data An array containing the data to put into the new tensor. String elements are 254 * sequences of bytes from the last array dimension. 255 */ 256 public static Tensor<String> create(byte[] data) { 257 return Tensor.create(data, String.class); 258 } 259 260 /** 261 * Creates a rank-1 tensor of {@code byte} elements. 262 * 263 * @param data An array containing the data to put into the new tensor. String elements are 264 * sequences of bytes from the last array dimension. 265 */ 266 public static Tensor<String> create(byte[][] data) { 267 return Tensor.create(data, String.class); 268 } 269 270 /** 271 * Creates a rank-2 tensor of {@code byte} elements. 272 * 273 * @param data An array containing the data to put into the new tensor. String elements are 274 * sequences of bytes from the last array dimension. 275 */ 276 public static Tensor<String> create(byte[][][] data) { 277 return Tensor.create(data, String.class); 278 } 279 280 /** 281 * Creates a rank-3 tensor of {@code byte} elements. 282 * 283 * @param data An array containing the data to put into the new tensor. String elements are 284 * sequences of bytes from the last array dimension. 285 */ 286 public static Tensor<String> create(byte[][][][] data) { 287 return Tensor.create(data, String.class); 288 } 289 290 /** 291 * Creates a rank-4 tensor of {@code byte} elements. 292 * 293 * @param data An array containing the data to put into the new tensor. String elements are 294 * sequences of bytes from the last array dimension. 295 */ 296 public static Tensor<String> create(byte[][][][][] data) { 297 return Tensor.create(data, String.class); 298 } 299 300 /** 301 * Creates a rank-5 tensor of {@code byte} elements. 302 * 303 * @param data An array containing the data to put into the new tensor. String elements are 304 * sequences of bytes from the last array dimension. 305 */ 306 public static Tensor<String> create(byte[][][][][][] data) { 307 return Tensor.create(data, String.class); 308 } 309 310 /** 311 * Creates a scalar tensor containing a single {@code long} element. 312 * 313 * @param data The value to put into the new scalar tensor. 314 */ 315 public static Tensor<Long> create(long data) { 316 return Tensor.create(data, Long.class); 317 } 318 319 /** 320 * Creates a rank-1 tensor of {@code long} elements. 321 * 322 * @param data An array containing the values to put into the new tensor. The dimensions of the 323 * new tensor will match those of the array. 324 */ 325 public static Tensor<Long> create(long[] data) { 326 return Tensor.create(data, Long.class); 327 } 328 329 /** 330 * Creates a rank-2 tensor of {@code long} elements. 331 * 332 * @param data An array containing the values to put into the new tensor. The dimensions of the 333 * new tensor will match those of the array. 334 */ 335 public static Tensor<Long> create(long[][] data) { 336 return Tensor.create(data, Long.class); 337 } 338 339 /** 340 * Creates a rank-3 tensor of {@code long} elements. 341 * 342 * @param data An array containing the values to put into the new tensor. The dimensions of the 343 * new tensor will match those of the array. 344 */ 345 public static Tensor<Long> create(long[][][] data) { 346 return Tensor.create(data, Long.class); 347 } 348 349 /** 350 * Creates a rank-4 tensor of {@code long} elements. 351 * 352 * @param data An array containing the values to put into the new tensor. The dimensions of the 353 * new tensor will match those of the array. 354 */ 355 public static Tensor<Long> create(long[][][][] data) { 356 return Tensor.create(data, Long.class); 357 } 358 359 /** 360 * Creates a rank-5 tensor of {@code long} elements. 361 * 362 * @param data An array containing the values to put into the new tensor. The dimensions of the 363 * new tensor will match those of the array. 364 */ 365 public static Tensor<Long> create(long[][][][][] data) { 366 return Tensor.create(data, Long.class); 367 } 368 369 /** 370 * Creates a rank-6 tensor of {@code long} elements. 371 * 372 * @param data An array containing the values to put into the new tensor. The dimensions of the 373 * new tensor will match those of the array. 374 */ 375 public static Tensor<Long> create(long[][][][][][] data) { 376 return Tensor.create(data, Long.class); 377 } 378 379 /** 380 * Creates a scalar tensor containing a single {@code boolean} element. 381 * 382 * @param data The value to put into the new scalar tensor. 383 */ 384 public static Tensor<Boolean> create(boolean data) { 385 return Tensor.create(data, Boolean.class); 386 } 387 388 /** 389 * Creates a rank-1 tensor of {@code boolean} elements. 390 * 391 * @param data An array containing the values to put into the new tensor. The dimensions of the 392 * new tensor will match those of the array. 393 */ 394 public static Tensor<Boolean> create(boolean[] data) { 395 return Tensor.create(data, Boolean.class); 396 } 397 398 /** 399 * Creates a rank-2 tensor of {@code boolean} elements. 400 * 401 * @param data An array containing the values to put into the new tensor. The dimensions of the 402 * new tensor will match those of the array. 403 */ 404 public static Tensor<Boolean> create(boolean[][] data) { 405 return Tensor.create(data, Boolean.class); 406 } 407 408 /** 409 * Creates a rank-3 tensor of {@code boolean} elements. 410 * 411 * @param data An array containing the values to put into the new tensor. The dimensions of the 412 * new tensor will match those of the array. 413 */ 414 public static Tensor<Boolean> create(boolean[][][] data) { 415 return Tensor.create(data, Boolean.class); 416 } 417 418 /** 419 * Creates a rank-4 tensor of {@code boolean} elements. 420 * 421 * @param data An array containing the values to put into the new tensor. The dimensions of the 422 * new tensor will match those of the array. 423 */ 424 public static Tensor<Boolean> create(boolean[][][][] data) { 425 return Tensor.create(data, Boolean.class); 426 } 427 428 /** 429 * Creates a rank-5 tensor of {@code boolean} elements. 430 * 431 * @param data An array containing the values to put into the new tensor. The dimensions of the 432 * new tensor will match those of the array. 433 */ 434 public static Tensor<Boolean> create(boolean[][][][][] data) { 435 return Tensor.create(data, Boolean.class); 436 } 437 438 /** 439 * Creates a rank-6 tensor of {@code boolean} elements. 440 * 441 * @param data An array containing the values to put into the new tensor. The dimensions of the 442 * new tensor will match those of the array. 443 */ 444 public static Tensor<Boolean> create(boolean[][][][][][] data) { 445 return Tensor.create(data, Boolean.class); 446 } 447 } 448