0
# Core Framework
1
2
The core framework types provide the fundamental data structures for TensorFlow graphs, tensors, operations, and computational definitions. These are the primary building blocks for representing TensorFlow computation graphs and tensor data.
3
4
## Capabilities
5
6
### TensorProto
7
8
Protocol buffer representing a tensor with data and metadata.
9
10
```java { .api }
11
/**
12
* Protocol buffer representing a tensor with data and metadata
13
*/
14
class TensorProto {
15
/** Get the tensor data type */
16
DataType getDtype();
17
18
/** Get the shape of the tensor */
19
TensorShapeProto getTensorShape();
20
21
/** Get version number for compatibility */
22
int getVersionNumber();
23
24
/** Get serialized raw tensor content */
25
ByteString getTensorContent();
26
27
/** Get float values for DT_FLOAT tensors */
28
List<Float> getFloatValList();
29
30
/** Get integer values for DT_INT32 tensors */
31
List<Integer> getIntValList();
32
33
/** Get double values for DT_DOUBLE tensors */
34
List<Double> getDoubleValList();
35
36
/** Get string values for DT_STRING tensors */
37
List<ByteString> getStringValList();
38
39
/** Get boolean values for DT_BOOL tensors */
40
List<Boolean> getBoolValList();
41
42
/** Get long values for DT_INT64 tensors */
43
List<Long> getInt64ValList();
44
45
/** Get half precision values for DT_HALF tensors */
46
List<Integer> getHalfValList();
47
48
/** Get single-precision complex values for DT_COMPLEX64 tensors */
49
List<Float> getSComplexValList();
50
51
/** Get double-precision complex values for DT_COMPLEX128 tensors */
52
List<Double> getDComplexValList();
53
54
/** Get unsigned 32-bit integer values for DT_UINT32 tensors */
55
List<Integer> getUint32ValList();
56
57
/** Get unsigned 64-bit integer values for DT_UINT64 tensors */
58
List<Long> getUint64ValList();
59
60
/** Get resource handle values for DT_RESOURCE tensors */
61
List<ResourceHandleProto> getResourceHandleValList();
62
63
/** Get variant tensor data for DT_VARIANT tensors */
64
List<VariantTensorDataProto> getVariantValList();
65
66
/** Create a new builder for constructing TensorProto */
67
static Builder newBuilder();
68
69
/** Builder for constructing TensorProto instances */
70
static class Builder {
71
Builder setDtype(DataType dtype);
72
Builder setTensorShape(TensorShapeProto shape);
73
Builder setVersionNumber(int version);
74
Builder setTensorContent(ByteString content);
75
Builder addFloatVal(float value);
76
Builder addIntVal(int value);
77
Builder addDoubleVal(double value);
78
Builder addStringVal(ByteString value);
79
Builder addBoolVal(boolean value);
80
Builder addInt64Val(long value);
81
Builder addHalfVal(int value);
82
Builder addSComplexVal(float value);
83
Builder addDComplexVal(double value);
84
Builder addUint32Val(int value);
85
Builder addUint64Val(long value);
86
Builder addResourceHandleVal(ResourceHandleProto value);
87
Builder addVariantVal(VariantTensorDataProto value);
88
TensorProto build();
89
}
90
}
91
```
92
93
**Usage Examples:**
94
95
```java
96
import org.tensorflow.framework.*;
97
98
// Create a float tensor with shape [3]
99
TensorProto floatTensor = TensorProto.newBuilder()
100
.setDtype(DataType.DT_FLOAT)
101
.setTensorShape(TensorShapeProto.newBuilder()
102
.addDim(TensorShapeProto.Dim.newBuilder().setSize(3)))
103
.addFloatVal(1.0f)
104
.addFloatVal(2.0f)
105
.addFloatVal(3.0f)
106
.build();
107
108
// Create a string tensor
109
TensorProto stringTensor = TensorProto.newBuilder()
110
.setDtype(DataType.DT_STRING)
111
.setTensorShape(TensorShapeProto.newBuilder()
112
.addDim(TensorShapeProto.Dim.newBuilder().setSize(2)))
113
.addStringVal(ByteString.copyFromUtf8("hello"))
114
.addStringVal(ByteString.copyFromUtf8("world"))
115
.build();
116
117
// Create tensor from raw bytes (more efficient for large tensors)
118
ByteString rawData = ByteString.copyFrom(new byte[]{1, 2, 3, 4});
119
TensorProto rawTensor = TensorProto.newBuilder()
120
.setDtype(DataType.DT_UINT8)
121
.setTensorShape(TensorShapeProto.newBuilder()
122
.addDim(TensorShapeProto.Dim.newBuilder().setSize(4)))
123
.setTensorContent(rawData)
124
.build();
125
```
126
127
### GraphDef
128
129
Represents the complete computation graph of operations.
130
131
```java { .api }
132
/**
133
* Represents the computation graph of operations
134
*/
135
class GraphDef {
136
/** Get list of nodes in the graph */
137
List<NodeDef> getNodeList();
138
139
/** Get compatibility version information */
140
VersionDef getVersions();
141
142
/** Get deprecated single version field */
143
@Deprecated
144
int getVersion();
145
146
/** Get user-defined functions library */
147
FunctionDefLibrary getLibrary();
148
149
/** Create a new builder for constructing GraphDef */
150
static Builder newBuilder();
151
152
/** Builder for constructing GraphDef instances */
153
static class Builder {
154
Builder addNode(NodeDef node);
155
Builder addAllNode(Iterable<NodeDef> nodes);
156
Builder setVersions(VersionDef versions);
157
Builder setLibrary(FunctionDefLibrary library);
158
GraphDef build();
159
}
160
}
161
```
162
163
**Usage Examples:**
164
165
```java
166
import org.tensorflow.framework.*;
167
168
// Create a simple graph with input and operation nodes
169
GraphDef graph = GraphDef.newBuilder()
170
.addNode(NodeDef.newBuilder()
171
.setName("input")
172
.setOp("Placeholder")
173
.putAttr("dtype", AttrValue.newBuilder()
174
.setType(DataType.DT_FLOAT).build())
175
.putAttr("shape", AttrValue.newBuilder()
176
.setShape(TensorShapeProto.newBuilder()
177
.addDim(TensorShapeProto.Dim.newBuilder().setSize(-1))
178
.addDim(TensorShapeProto.Dim.newBuilder().setSize(784)))
179
.build()))
180
.addNode(NodeDef.newBuilder()
181
.setName("weights")
182
.setOp("Variable")
183
.putAttr("dtype", AttrValue.newBuilder()
184
.setType(DataType.DT_FLOAT).build())
185
.putAttr("shape", AttrValue.newBuilder()
186
.setShape(TensorShapeProto.newBuilder()
187
.addDim(TensorShapeProto.Dim.newBuilder().setSize(784))
188
.addDim(TensorShapeProto.Dim.newBuilder().setSize(10)))
189
.build()))
190
.addNode(NodeDef.newBuilder()
191
.setName("matmul")
192
.setOp("MatMul")
193
.addInput("input")
194
.addInput("weights"))
195
.setVersions(VersionDef.newBuilder()
196
.setProducer(26)
197
.setMinConsumer(12))
198
.build();
199
```
200
201
### NodeDef
202
203
Defines a single node (operation) in the computation graph.
204
205
```java { .api }
206
/**
207
* Defines a single node (operation) in the computation graph
208
*/
209
class NodeDef {
210
/** Get unique name of the node */
211
String getName();
212
213
/** Get operation name */
214
String getOp();
215
216
/** Get list of input tensor names */
217
List<String> getInputList();
218
219
/** Get device specification */
220
String getDevice();
221
222
/** Get operation attributes */
223
Map<String, AttrValue> getAttrMap();
224
225
/** Create a new builder for constructing NodeDef */
226
static Builder newBuilder();
227
228
/** Builder for constructing NodeDef instances */
229
static class Builder {
230
Builder setName(String name);
231
Builder setOp(String op);
232
Builder addInput(String input);
233
Builder addAllInput(Iterable<String> inputs);
234
Builder setDevice(String device);
235
Builder putAttr(String key, AttrValue value);
236
Builder putAllAttr(Map<String, AttrValue> attrs);
237
NodeDef build();
238
}
239
}
240
```
241
242
**Usage Examples:**
243
244
```java
245
import org.tensorflow.framework.*;
246
247
// Create a placeholder node
248
NodeDef placeholder = NodeDef.newBuilder()
249
.setName("input_data")
250
.setOp("Placeholder")
251
.putAttr("dtype", AttrValue.newBuilder()
252
.setType(DataType.DT_FLOAT).build())
253
.putAttr("shape", AttrValue.newBuilder()
254
.setShape(TensorShapeProto.newBuilder()
255
.addDim(TensorShapeProto.Dim.newBuilder().setSize(-1))
256
.addDim(TensorShapeProto.Dim.newBuilder().setSize(256)))
257
.build())
258
.build();
259
260
// Create a constant node
261
NodeDef constant = NodeDef.newBuilder()
262
.setName("learning_rate")
263
.setOp("Const")
264
.putAttr("dtype", AttrValue.newBuilder()
265
.setType(DataType.DT_FLOAT).build())
266
.putAttr("value", AttrValue.newBuilder()
267
.setTensor(TensorProto.newBuilder()
268
.setDtype(DataType.DT_FLOAT)
269
.addFloatVal(0.001f)
270
.setTensorShape(TensorShapeProto.newBuilder()))
271
.build())
272
.build();
273
274
// Create a binary operation node with inputs
275
NodeDef multiply = NodeDef.newBuilder()
276
.setName("scaled_output")
277
.setOp("Mul")
278
.addInput("dense_output")
279
.addInput("learning_rate")
280
.setDevice("/gpu:0") // Specify device
281
.build();
282
```
283
284
### OpDef
285
286
Defines the interface for an operation type.
287
288
```java { .api }
289
/**
290
* Defines the interface for an operation type
291
*/
292
class OpDef {
293
/** Get operation name */
294
String getName();
295
296
/** Get input argument specifications */
297
List<ArgDef> getInputArgList();
298
299
/** Get output argument specifications */
300
List<ArgDef> getOutputArgList();
301
302
/** Get attribute definitions */
303
List<AttrDef> getAttrList();
304
305
/** Get one-line description */
306
String getSummary();
307
308
/** Get detailed description */
309
String getDescription();
310
311
/** Check if operation is stateful (has side effects) */
312
boolean getIsStateful();
313
314
/** Check if operation is commutative */
315
boolean getIsCommutative();
316
317
/** Check if operation is aggregate (like Sum, Max) */
318
boolean getIsAggregate();
319
320
/**
321
* Argument definition for inputs and outputs
322
*/
323
static class ArgDef {
324
String getName();
325
String getDescription();
326
DataType getType();
327
String getTypeAttr();
328
String getNumberAttr();
329
String getTypeListAttr();
330
boolean getIsRef();
331
}
332
333
/**
334
* Attribute definition
335
*/
336
static class AttrDef {
337
String getName();
338
String getType();
339
String getDescription();
340
AttrValue getDefaultValue();
341
boolean getHasMinimum();
342
long getMinimum();
343
AttrValue getAllowedValues();
344
}
345
}
346
```
347
348
### FunctionDef
349
350
Defines a reusable function in TensorFlow.
351
352
```java { .api }
353
/**
354
* Defines a reusable function in TensorFlow
355
*/
356
class FunctionDef {
357
/** Get function signature */
358
OpDef getSignature();
359
360
/** Get function body nodes */
361
List<NodeDef> getNodeDefList();
362
363
/** Get return value mapping from output name to node:output */
364
Map<String, String> getRetMap();
365
366
/** Get function attributes */
367
Map<String, AttrValue> getAttrMap();
368
369
/** Get argument attributes */
370
Map<String, ArgAttrs> getArgAttrMap();
371
372
/** Attributes for function arguments */
373
static class ArgAttrs {
374
Map<String, AttrValue> getAttrMap();
375
}
376
}
377
```
378
379
### FunctionDefLibrary
380
381
Library of function definitions for reusable operations.
382
383
```java { .api }
384
/**
385
* Library of function definitions
386
*/
387
class FunctionDefLibrary {
388
/** Get list of function definitions */
389
List<FunctionDef> getFunctionList();
390
391
/** Get list of gradient definitions */
392
List<GradientDef> getGradientList();
393
394
/** Create a new builder */
395
static Builder newBuilder();
396
397
static class Builder {
398
Builder addFunction(FunctionDef function);
399
Builder addAllFunction(Iterable<FunctionDef> functions);
400
Builder addGradient(GradientDef gradient);
401
FunctionDefLibrary build();
402
}
403
}
404
```
405
406
### GradientDef
407
408
Defines gradient computation for a function.
409
410
```java { .api }
411
/**
412
* Gradient definition for a function
413
*/
414
class GradientDef {
415
/** Get function name this gradient applies to */
416
String getFunctionName();
417
418
/** Get gradient function name */
419
String getGradientFunc();
420
421
/** Create a new builder */
422
static Builder newBuilder();
423
424
static class Builder {
425
Builder setFunctionName(String name);
426
Builder setGradientFunc(String func);
427
GradientDef build();
428
}
429
}
430
```
431
432
### VersionDef
433
434
Version compatibility information for graphs.
435
436
```java { .api }
437
/**
438
* Version compatibility information
439
*/
440
class VersionDef {
441
/** Producer version that created this graph */
442
int getProducer();
443
444
/** Minimum consumer version that can read this graph */
445
int getMinConsumer();
446
447
/** Consumer versions with known incompatibilities */
448
List<Integer> getBadConsumersList();
449
}
450
```
451
452
### Device and Performance Types
453
454
```java { .api }
455
/**
456
* Device attributes and capabilities
457
*/
458
class DeviceAttributes {
459
String getName();
460
String getDeviceType();
461
long getMemoryLimit();
462
DeviceLocality getLocality();
463
long getIncarnation();
464
String getPhysicalDeviceDesc();
465
}
466
467
/**
468
* Step execution statistics
469
*/
470
class StepStats {
471
List<DeviceStepStats> getDevStatssList();
472
473
static class DeviceStepStats {
474
String getDevice();
475
List<NodeExecStats> getNodeStatsList();
476
}
477
478
/** Execution statistics for a single node */
479
static class NodeExecStats {
480
String getNodeName();
481
long getAllStartMicros();
482
long getOpStartRelMicros();
483
long getOpEndRelMicros();
484
long getAllEndRelMicros();
485
List<AllocatorMemoryUsed> getMemoryList();
486
List<NodeOutput> getOutputList();
487
String getTimelineLabel();
488
long getScheduledMicros();
489
int getThreadId();
490
}
491
}
492
```
493
494
### VariantTensorDataProto
495
496
Protocol buffer representing variant tensor data for DT_VARIANT tensors.
497
498
```java { .api }
499
/**
500
* Serialization format for DT_VARIANT tensors
501
*/
502
class VariantTensorDataProto {
503
/** Get the type name of objects being serialized */
504
String getTypeName();
505
506
/** Get serialized metadata (non-tensor portions) */
507
ByteString getMetadata();
508
509
/** Get tensors contained within the variant */
510
List<TensorProto> getTensorsList();
511
512
/** Create a new builder */
513
static Builder newBuilder();
514
515
static class Builder {
516
Builder setTypeName(String typeName);
517
Builder setMetadata(ByteString metadata);
518
Builder addTensors(TensorProto tensor);
519
Builder addAllTensors(Iterable<TensorProto> tensors);
520
VariantTensorDataProto build();
521
}
522
}
523
```
524
525
### ResourceHandleProto
526
527
Protocol buffer representing a handle to a TensorFlow resource.
528
529
```java { .api }
530
/**
531
* Handle to a TensorFlow resource (not valid across executions)
532
*/
533
class ResourceHandleProto {
534
/** Get device containing the resource */
535
String getDevice();
536
537
/** Get container in which resource is placed */
538
String getContainer();
539
540
/** Get unique name of this resource */
541
String getName();
542
543
/** Get hash code for the resource type */
544
long getHashCode();
545
546
/** Get debug type name (if available) */
547
String getMaybeTypeName();
548
549
/** Create a new builder */
550
static Builder newBuilder();
551
552
static class Builder {
553
Builder setDevice(String device);
554
Builder setContainer(String container);
555
Builder setName(String name);
556
Builder setHashCode(long hashCode);
557
Builder setMaybeTypeName(String typeName);
558
ResourceHandleProto build();
559
}
560
}
561
```