aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/g3doc/api_docs/python/constant_op.md
blob: 34d2b511abc3ab57a5a8746446667785d7f5dc16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<!-- This file is machine generated: DO NOT EDIT! -->

# Constants, Sequences, and Random Values
<!-- TOC-BEGIN This section is generated by neural network: DO NOT EDIT! -->
## Contents
* [Constant Value Tensors](#AUTOGENERATED-constant-value-tensors)
  * [tf.zeros(shape, dtype=tf.float32, name=None)](#zeros)
  * [tf.zeros_like(tensor, dtype=None, name=None)](#zeros_like)
  * [tf.ones(shape, dtype=tf.float32, name=None)](#ones)
  * [tf.ones_like(tensor, dtype=None, name=None)](#ones_like)
  * [tf.fill(dims, value, name=None)](#fill)
  * [tf.constant(value, dtype=None, shape=None, name='Const')](#constant)
* [Sequences](#AUTOGENERATED-sequences)
  * [tf.linspace(start, stop, num, name=None)](#linspace)
  * [tf.range(start, limit, delta=1, name='range')](#range)
* [Random Tensors](#AUTOGENERATED-random-tensors)
  * [Examples:](#AUTOGENERATED-examples-)
  * [tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)](#random_normal)
  * [tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)](#truncated_normal)
  * [tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None)](#random_uniform)
  * [tf.random_shuffle(value, seed=None, name=None)](#random_shuffle)
  * [tf.set_random_seed(seed)](#set_random_seed)


<!-- TOC-END This section was generated by neural network, THANKS FOR READING! -->

## Constant Value Tensors <div class="md-anchor" id="AUTOGENERATED-constant-value-tensors">{#AUTOGENERATED-constant-value-tensors}</div>

TensorFlow provides several operations that you can use to generate constants.

- - -

### tf.zeros(shape, dtype=tf.float32, name=None) <div class="md-anchor" id="zeros">{#zeros}</div>

Creates a tensor with all elements set to zero.

This operation returns a tensor of type `dtype` with shape `shape` and
all elements set to zero.

For example:

```python
tf.zeros([3, 4], int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
```

##### Args:


*  <b>shape</b>: Either a list of integers, or a 1-D `Tensor` of type `int32`.
*  <b>dtype</b>: The type of an element in the resulting `Tensor`.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A `Tensor` with all elements set to zero.


- - -

### tf.zeros_like(tensor, dtype=None, name=None) <div class="md-anchor" id="zeros_like">{#zeros_like}</div>

Creates a tensor with all elements set to zero.

Given a single tensor (`tensor`), this operation returns a tensor of the
same type and shape as `tensor` with all elements set to zero. Optionally,
you can use `dtype` to specify a new type for the returned tensor.

For example:

```python
# 'tensor' is [[1, 2, 3], [4, 5, 6]]
tf.zeros_like(tensor) ==> [[0, 0, 0], [0, 0, 0]]
```

##### Args:


*  <b>tensor</b>: A `Tensor`.
*  <b>dtype</b>: A type for the returned `Tensor`. Must be `float32`, `float64`,
  `int8`, `int16`, `int32`, `int64`, `uint8`, or `complex64`.

*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A `Tensor` with all elements set to zero.



- - -

### tf.ones(shape, dtype=tf.float32, name=None) <div class="md-anchor" id="ones">{#ones}</div>

Creates a tensor with all elements set to 1.

This operation returns a tensor of type `dtype` with shape `shape` and all
elements set to 1.

For example:

```python
tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]
```

##### Args:


*  <b>shape</b>: Either a list of integers, or a 1-D `Tensor` of type `int32`.
*  <b>dtype</b>: The type of an element in the resulting `Tensor`.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A `Tensor` with all elements set to 1.


- - -

### tf.ones_like(tensor, dtype=None, name=None) <div class="md-anchor" id="ones_like">{#ones_like}</div>

Creates a tensor with all elements set to 1.

Given a single tensor (`tensor`), this operation returns a tensor of the same
type and shape as `tensor` with all elements set to 1. Optionally, you can
specify a new type (`dtype`) for the returned tensor.

For example:

```python
# 'tensor' is [[1, 2, 3], [4, 5, 6]]
tf.ones_like(tensor) ==> [[1, 1, 1], [1, 1, 1]]
```

##### Args:


*  <b>tensor</b>: A `Tensor`.
*  <b>dtype</b>: A type for the returned `Tensor`. Must be `float32`, `float64`,
  `int8`, `int16`, `int32`, `int64`, `uint8`, or `complex64`.

*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A `Tensor` with all elements set to 1.



- - -

### tf.fill(dims, value, name=None) <div class="md-anchor" id="fill">{#fill}</div>

Creates a tensor filled with a scalar value.

This operation creates a tensor of shape `dims` and fills it with `value`.

For example:

```prettyprint
# output tensor shape needs to be [2, 3]
# so 'dims' is [2, 3]
fill(dims, 9) ==> [[9, 9, 9]
                   [9, 9, 9]]
```

##### Args:


*  <b>dims</b>: A `Tensor` of type `int32`.
    1-D. Represents the shape of the output tensor.
*  <b>value</b>: A `Tensor`. 0-D (scalar). Value to fill the returned tensor.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A `Tensor`. Has the same type as `value`.



- - -

### tf.constant(value, dtype=None, shape=None, name='Const') <div class="md-anchor" id="constant">{#constant}</div>

Creates a constant tensor.

 The resulting tensor is populated with values of type `dtype`, as
 specified by arguments `value` and (optionally) `shape` (see examples
 below).

 The argument `value` can be a constant value, or a list of values of type
 `dtype`. If `value` is a list, then the length of the list must be less
 than or equal to the number of elements implied by the `shape` argument (if
 specified). In the case where the list length is less than the number of
 elements specified by `shape`, the last element in the list will be used
 to fill the remaining entries.

 The argument `shape` is optional. If present, it specifies the dimensions
 of the resulting tensor. If not present, then the tensor is a scalar (0-D)
 if `value` is a scalar, or 1-D otherwise.

 If the argument `dtype` is not specified, then the type is inferred from
 the type of `value`.

 For example:

 ```python
 # Constant 1-D Tensor populated with value list.
 tensor = tf.constant([1, 2, 3, 4, 5, 6, 7]) => [1 2 3 4 5 6 7]

 # Constant 2-D tensor populated with scalar value -1.
 tensor = tf.constant(-1.0, shape=[2, 3]) => [[-1. -1. -1.]
                                              [-1. -1. -1.]]
 ```

##### Args:


*  <b>value</b>: A constant value (or list) of output type `dtype`.


*  <b>dtype</b>: The type of the elements of the resulting tensor.


*  <b>shape</b>: Optional dimensions of resulting tensor.


*  <b>name</b>: Optional name for the tensor.

##### Returns:

  A Constant Tensor.



## Sequences <div class="md-anchor" id="AUTOGENERATED-sequences">{#AUTOGENERATED-sequences}</div>

- - -

### tf.linspace(start, stop, num, name=None) <div class="md-anchor" id="linspace">{#linspace}</div>

Generates values in an interval.

A sequence of `num` evenly-spaced values are generated beginning at `start`.
If `num > 1`, the values in the sequence increase by `stop - start / num - 1`,
so that the last one is exactly `stop`.

For example:

```
tf.linspace(10.0, 12.0, 3, name="linspace") => [ 10.0  11.0  12.0]
```

##### Args:


*  <b>start</b>: A `Tensor`. Must be one of the following types: `float32`, `float64`.
    First entry in the range.
*  <b>stop</b>: A `Tensor`. Must have the same type as `start`.
    Last entry in the range.
*  <b>num</b>: A `Tensor` of type `int32`. Number of values to generate.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A `Tensor`. Has the same type as `start`. 1-D. The generated values.



- - -

### tf.range(start, limit, delta=1, name='range') <div class="md-anchor" id="range">{#range}</div>

Creates a sequence of integers.

This operation creates a sequence of integers that begins at `start` and
extends by increments of `delta` up to but not including `limit`.

For example:

```
# 'start' is 3
# 'limit' is 18
# 'delta' is 3
tf.range(start, limit, delta) ==> [3, 6, 9, 12, 15]
```

##### Args:


*  <b>start</b>: A 0-D (scalar) of type `int32`. First entry in sequence.
*  <b>limit</b>: A 0-D (scalar) of type `int32`. Upper limit of sequence,
    exclusive.
*  <b>delta</b>: A 0-D `Tensor` (scalar) of type `int32`. Optional. Default is 1.
    Number that increments `start`.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  An 1-D `int32` `Tensor`.



## Random Tensors <div class="md-anchor" id="AUTOGENERATED-random-tensors">{#AUTOGENERATED-random-tensors}</div>

TensorFlow has several ops that create random tensors with different
distributions.  The random ops are stateful, and create new random values each
time they are evaluated.

The `seed` keyword argument in these functions acts in conjunction with
the graph-level random seed. Changing either the graph-level seed using
[`set_random_seed`](constant_op.md#set_random_seed) or the op-level seed
will change the underlying seed of these operations. Setting neither graph-level
nor op-level seed, results in a random seed for all operations.
See [`set_random_seed`](constant_op.md#set_random_seed) for details on the
interaction between operation-level and graph-level random seeds.

### Examples: <div class="md-anchor" id="AUTOGENERATED-examples-">{#AUTOGENERATED-examples-}</div>

```python
# Create a tensor of shape [2, 3] consisting of random normal values, with mean
# -1 and standard deviation 4.
norm = tf.random_normal([2, 3], mean=-1, stddev=4)

# Shuffle the first dimension of a tensor
c = tf.constant([[1, 2], [3, 4], [5, 6]])
shuff = tf.random_shuffle(c)

# Each time we run these ops, different results are generated
sess = tf.Session()
print sess.run(norm)
print sess.run(norm)

# Set an op-level seed to generate repeatable sequences across sessions.
c = tf.constant([[1, 2], [3, 4], [5, 6]])
sess = tf.Session()
norm = tf.random_normal(c, seed=1234)
print sess.run(norm)
print sess.run(norm)
```

Another common use of random values is the intialization of variables. Also see
the [Variables How To](../../how_tos/variables/index.md).

```python
# Use random uniform values in [0, 1) as the initializer for a variable of shape
# [2, 3]. The default type is float32.
var = tf.Variable(tf.random_uniform([2, 3]), name="var")
init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)
print sess.run(var)
```

- - -

### tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) <div class="md-anchor" id="random_normal">{#random_normal}</div>

Outputs random values from a normal distribution.

##### Args:


*  <b>shape</b>: A 1-D integer Tensor or Python array. The shape of the output tensor.
*  <b>mean</b>: A 0-D Tensor or Python value of type `dtype`. The mean of the normal
    distribution.
*  <b>stddev</b>: A 0-D Tensor or Python value of type `dtype`. The standard deviation
    of the normal distribution.
*  <b>dtype</b>: The type of the output.
*  <b>seed</b>: A Python integer. Used to create a random seed for the distribution.
    See [`set_random_seed`](constant_op.md#set_random_seed) for behavior.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A tensor of the specified shape filled with random normal values.


- - -

### tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) <div class="md-anchor" id="truncated_normal">{#truncated_normal}</div>

Outputs random values from a truncated normal distribution.

The generated values follow a normal distribution with specified mean and
standard deviation, except that values whose magnitude is more than 2 standard
deviations from the mean are dropped and re-picked.

##### Args:


*  <b>shape</b>: A 1-D integer Tensor or Python array. The shape of the output tensor.
*  <b>mean</b>: A 0-D Tensor or Python value of type `dtype`. The mean of the
    truncated normal distribution.
*  <b>stddev</b>: A 0-D Tensor or Python value of type `dtype`. The standard deviation
    of the truncated normal distribution.
*  <b>dtype</b>: The type of the output.
*  <b>seed</b>: A Python integer. Used to create a random seed for the distribution.
    See [`set_random_seed`](constant_op.md#set_random_seed) for behavior.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A tensor of the specified shape filled with random truncated normal values.


- - -

### tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None) <div class="md-anchor" id="random_uniform">{#random_uniform}</div>

Outputs random values from a uniform distribution.

The generated values follow a uniform distribution in the range
`[minval, maxval)`. The lower bound `minval` is included in the range, while
the upper bound `maxval` is excluded.

##### Args:


*  <b>shape</b>: A 1-D integer Tensor or Python array. The shape of the output tensor.
*  <b>minval</b>: A 0-D Tensor or Python value of type `dtype`. The lower bound on the
    range of random values to generate.
*  <b>maxval</b>: A 0-D Tensor or Python value of type `dtype`. The upper bound on
    the range of random values to generate.
*  <b>dtype</b>: The type of the output.
*  <b>seed</b>: A Python integer. Used to create a random seed for the distribution.
    See [`set_random_seed`](constant_op.md#set_random_seed) for behavior.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A tensor of the specified shape filled with random uniform values.


- - -

### tf.random_shuffle(value, seed=None, name=None) <div class="md-anchor" id="random_shuffle">{#random_shuffle}</div>

Randomly shuffles a tensor along its first dimension.

The tensor is shuffled along dimension 0, such that each `value[j]` is mapped
to one and only one `output[i]`. For example, a mapping that might occur for a
3x2 tensor is:

```python
[[1, 2],       [[5, 6],
 [3, 4],  ==>   [1, 2],
 [5, 6]]        [3, 4]]
```

##### Args:


*  <b>value</b>: A Tensor to be shuffled.
*  <b>seed</b>: A Python integer. Used to create a random seed for the distribution.
    See [`set_random_seed`](constant_op.md#set_random_seed) for behavior.
*  <b>name</b>: A name for the operation (optional).

##### Returns:

  A tensor of same shape and type as `value`, shuffled along its first
  dimension.


- - -

### tf.set_random_seed(seed) <div class="md-anchor" id="set_random_seed">{#set_random_seed}</div>

Sets the graph-level random seed.

Operations that rely on a random seed actually derive it from two seeds:
the graph-level and operation-level seeds. This sets the graph-level seed.

Its interactions with operation-level seeds is as follows:

  1. If neither the graph-level nor the operation seed is set:
    A random seed is used for this op.
  2. If the graph-level seed is set, but the operation seed is not:
    The system deterministically picks an operation seed in conjunction
    with the graph-level seed so that it gets a unique random sequence.
  3. If the graph-level seed is not set, but the operation seed is set:
    A default graph-level seed and the specified operation seed are used to
    determine the random sequence.
  4. If both the graph-level and the operation seed are set:
    Both seeds are used in conjunction to determine the random sequence.

To illustrate the user-visible effects, consider these examples:

To generate different sequences across sessions, set neither
graph-level nor op-level seeds:

```python
a = tf.random_uniform([1])
b = tf.random_normal([1])

print "Session 1"
with tf.Session() as sess1:
  print sess1.run(a)  # generates 'A1'
  print sess1.run(a)  # generates 'A2'
  print sess1.run(b)  # generates 'B1'
  print sess1.run(b)  # generates 'B2'

print "Session 2"
with tf.Session() as sess2:
  print sess2.run(a)  # generates 'A3'
  print sess2.run(a)  # generates 'A4'
  print sess2.run(b)  # generates 'B3'
  print sess2.run(b)  # generates 'B4'
```

To generate the same repeatable sequence for an op across sessions, set the
seed for the op:

```python
a = tf.random_uniform([1], seed=1)
b = tf.random_normal([1])

# Repeatedly running this block with the same graph will generate the same
# sequence of values for 'a', but different sequences of values for 'b'.
print "Session 1"
with tf.Session() as sess1:
  print sess1.run(a)  # generates 'A1'
  print sess1.run(a)  # generates 'A2'
  print sess1.run(b)  # generates 'B1'
  print sess1.run(b)  # generates 'B2'

print "Session 2"
with tf.Session() as sess2:
  print sess2.run(a)  # generates 'A1'
  print sess2.run(a)  # generates 'A2'
  print sess2.run(b)  # generates 'B3'
  print sess2.run(b)  # generates 'B4'
```

To make the random sequences generated by all ops be repeatable across
sessions, set a graph-level seed:

```python
tf.set_random_seed(1234)
a = tf.random_uniform([1])
b = tf.random_normal([1])

# Repeatedly running this block with the same graph will generate different
# sequences of 'a' and 'b'.
print "Session 1"
with tf.Session() as sess1:
  print sess1.run(a)  # generates 'A1'
  print sess1.run(a)  # generates 'A2'
  print sess1.run(b)  # generates 'B1'
  print sess1.run(b)  # generates 'B2'

print "Session 2"
with tf.Session() as sess2:
  print sess2.run(a)  # generates 'A1'
  print sess2.run(a)  # generates 'A2'
  print sess2.run(b)  # generates 'B1'
  print sess2.run(b)  # generates 'B2'
```

##### Args:


*  <b>seed</b>: integer.