2017-02-20 10 views
8

हाय मैं tensorflow के लिए नया हूँ। मैं tensorflow में निम्नलिखित पायथन कोड को लागू करना चाहता हूँ।tensorflow में numpy.newaxis का विकल्प क्या है?

import numpy as np 
a = np.array([1,2,3,4,5,6,7,9,0]) 
print(a) ## [1 2 3 4 5 6 7 9 0] 
print(a.shape) ## (9,) 
b = a[:, np.newaxis] ### want to write this in tensorflow. 
print(b.shape) ## (9,1) 

उत्तर

8

मुझे लगता है कि tf.expand_dims होगा -

tf.expand_dims(a, 1) # Or tf.expand_dims(a, -1) 

असल में, हम अक्ष आईडी सूची जहां इस नई धुरी डाला जा रहा है और अनुगामी अक्ष/मंद धक्का दिया वापस हैं।

जुड़ा हुआ डॉक्स से, यहाँ के विस्तार आयामों के कुछ उदाहरण है -

# 't' is a tensor of shape [2] 
shape(expand_dims(t, 0)) ==> [1, 2] 
shape(expand_dims(t, 1)) ==> [2, 1] 
shape(expand_dims(t, -1)) ==> [2, 1] 

# 't2' is a tensor of shape [2, 3, 5] 
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5] 
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5] 
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1] 
+0

धन्यवाद। इसने काम कर दिया – Rahul

2

इसी आदेश tf.newaxis है। इसमें टेंसफोर्लो के दस्तावेज में स्वयं का कोई प्रविष्टि नहीं है, लेकिन tf.stride_slice के दस्तावेज़ पृष्ठ पर संक्षेप में इसका उल्लेख किया गया है।

x = tf.ones((10,10,10)) 
y = x[:,tf.newaxis,...] 
print(y.shape) 
# prints (10, 1, 10, 10) 

के रूप में ऊपर के लिंक में कहा गया है tf.expand_dims का उपयोग करना भी ठीक है, लेकिन,,

उन इंटरफेस और अधिक अनुकूल है, और अत्यधिक की सिफारिश की है।

0

आप बिल्कुल वैसा ही (यानी। None) प्रकार NumPy के रूप में रुचि रखते हैं, तो tf.newaxisnp.newaxis के लिए सटीक विकल्प नहीं है।

उदाहरण:

In [71]: a1 = tf.constant([2,2], name="a1") 

In [72]: a1 
Out[72]: <tf.Tensor 'a1_5:0' shape=(2,) dtype=int32> 

# add a new dimension 
In [73]: a1_new = a1[tf.newaxis, :] 

In [74]: a1_new 
Out[74]: <tf.Tensor 'strided_slice_5:0' shape=(1, 2) dtype=int32> 

# add one more dimension 
In [75]: a1_new = a1[tf.newaxis, :, tf.newaxis] 

In [76]: a1_new 
Out[76]: <tf.Tensor 'strided_slice_6:0' shape=(1, 2, 1) dtype=int32> 

यही कार्य है कि आप NumPy में क्या उसी तरह की है। बस उसी आयाम पर इसका उपयोग करें जहां आप इसे बढ़ाना चाहते हैं।

संबंधित मुद्दे