본문 바로가기
  • Let's go grab a data
Data/TensorFlow

미분계수구하기, 난수 발생

by pub-lican-ai 2017. 3. 16.
반응형


미분계수구하기, 난수 발생


[미분계수 구하기]

import tensorflow as tf

import matplotlib.pylab as plt

#미분계수 구하기 tf.gradients 함수이용

x=tf.placeholder(tf.float32)

y=2*x*x

var_grad=tf.gradients(y,x)

with tf.Session() as session:

    var_grad_val= session.run(var_grad,feed_dict={x:1})

print(var_grad_val)

[4.0]


[난수 발생]

#난수 - 균일분포

uniform = tf.random_uniform([100],minval=0,maxval=1,dtype=tf.float32)  #1차원텐서로 100개의 값이 0~1사이의 동일한 확률로 분포됨

with tf.Session() as session:

    print(uniform.eval())

    plt.hist(uniform.eval(),normed=True)

    plt.show()


[ 0.20909667  0.37979138  0.42030847  0.01011443  0.80482483  0.65402865
  0.06825662  0.52520657  0.72236359  0.25463879  0.05504858  0.1794399
  0.90988982  0.13689888  0.77340961  0.16532195  0.25279379  0.03426957
  0.1758343   0.13750553  0.26735699  0.07289553  0.37042391  0.4742099
  0.61287785  0.03123951  0.76944423  0.72415042  0.40667367  0.26822841
  0.39977658  0.80704248  0.10717201  0.78052831  0.89647019  0.44822431
  0.84064257  0.24672198  0.16433191  0.76785219  0.67084181  0.28761661
  0.38928461  0.36094499  0.67015004  0.91841125  0.30247009  0.34405208
  0.41794336  0.22334099  0.76999652  0.22380579  0.19836605  0.35170054
  0.55554259  0.53432572  0.37656832  0.26515293  0.84705973  0.48974848
  0.15296209  0.68804491  0.35248744  0.12241983  0.58910394  0.77649546
  0.05925345  0.38345146  0.23783588  0.36278319  0.8750608   0.18566763
  0.48135185  0.83014822  0.77763724  0.84236383  0.95817113  0.71449041
  0.09911895  0.61169302  0.4028821   0.99501479  0.61875427  0.155406
  0.47269499  0.00359094  0.74334121  0.89358127  0.45988882  0.89380431
  0.58076489  0.16845918  0.9234488   0.52839684  0.15146422  0.21612132
  0.18001032  0.62562168  0.18184769  0.63704336]


#난수 - 정규분포

norm= tf.random_normal([100],mean=0,stddev=2)  #1차원텐서로 100개의 값이 0~1사이의 정규분포를 보임

with tf.Session() as session:

    print(norm.eval())

    plt.hist(norm.eval(),normed=True)

    plt.show()


[-0.4644084   0.51059371 -0.69656146 -2.40202188 -0.94566137  0.27644902
  0.63445383 -0.61061358 -0.77215368 -1.73253047  0.37163389  0.45503333
  2.62802696  0.56287652 -1.58761013 -2.59392738  0.0999367   1.34591806
 -1.72527623 -0.82388085 -1.38869774  1.22838032  1.56175351  0.42012691
 -0.91809034 -1.34810948  1.52260041 -1.49175358  0.58695579 -0.3213098
  0.84111863 -0.54545277  0.06774808 -0.71714187  2.86919022  2.0914464
 -0.01483861  3.73724413 -1.36112881  1.52403784  1.8567934   0.59756428
 -1.20436597 -2.39589357  1.18054938  2.34276152  1.38167274  1.69013536
  3.19249129 -2.52079034 -1.76390922  4.62997341 -0.79311395  3.22813678
  1.53103745  0.12669744 -3.74720073  0.59902954 -0.67252707  0.14414319
 -3.43013453  1.10606205  1.48729551  0.56902784 -0.67229277 -0.93413687
 -1.93424869  0.22076537  0.04267672 -1.54198921  0.73521686  1.26687586
  0.45638725 -1.32211101  0.0824327   3.49682426  1.60448015 -0.37463355
 -3.41505885 -1.40330791 -0.5596258   2.5709486  -0.76236838  1.51821196
 -3.30255103 -1.34809887  0.28708988  0.54637986 -0.89080393 -1.67605281
 -2.24336171  1.62686539 -3.77078986 -0.15237968  2.29279613  0.37755895
 -0.55560148 -1.39829397  1.15071619  1.08339417]

#시드 사용 norm = tf.random_normal([100],mean=0,stddev=2,seed=1) #1차원텐서로 100개의 값이 0~1사이의 동일한 확률로 분포됨 with tf.Session() as session: print(norm.eval()) plt.hist(norm.eval(),normed=True) plt.show()

[ 2.12332416 -1.75715649  0.22374433  0.37023431  1.81845129 -1.47574472
  0.60030025  3.48577404 -0.12633109 -2.61546278  0.32027262 -2.90148258
 -4.09764862  4.37100124  2.24698257  2.60675573 -2.67038274 -1.90815973
 -0.52695525 -2.19444084  0.29134279 -0.71678275 -0.01690346 -4.56540871
 -1.78749347  1.8373816  -2.12405849 -0.46094915  1.93911326 -2.01236296
  0.03847122 -5.46110678 -1.03985178 -1.23548639 -3.28388739  0.51310945
  1.32826209  0.22826335 -1.13436341 -1.76840651  3.79177117 -0.47732946
  3.56917143  3.17304754  2.22271609 -0.79361081  3.31964087 -2.51143122
 -0.59751385 -0.28046337  1.57685697  1.66220284  5.16533756  0.81847346
  0.87785548 -0.70457125  0.30507347 -0.91539282  2.00575089  1.29362977
 -1.64281332 -2.13524914 -1.70883322 -0.0143463   1.6757046   0.42982188
 -0.08934316 -3.05559015 -3.64667916 -3.14650893  3.86695886 -3.14573336
 -2.91129041 -0.16611838  0.15759768  0.43741894 -1.84960485 -0.3422811
  3.06386447  0.30843222  2.20893645 -0.30526024 -1.76682925  0.10448162
 -2.2547698  -0.80683261 -0.96552145  1.80204654 -0.41187382  4.36971855
 -2.49812484  2.35278845 -0.38579372 -1.64777815  3.0833075  -2.72457981
  0.91168815 -0.33526367 -1.19342148 -1.3174988 ]

#몬테카를로 기법: 산술확률적 기법

#원의 면적은 ㅠ이며 균일분포 난수 범위는 면적이 4 (-1~1 * -1~1)인 정사각형에서 x제곱+y제곱이 1보다 작은 즉, 원 내부에 있는 점만 세면

#원의 면적에 수렴해감.

trials=100

hits=0

x= tf.random_uniform([1],minval=-1,maxval=1,dtype=tf.float32)

y= tf.random_uniform([1],minval=-1,maxval=1,dtype=tf.float32)

pi=[]

sess = tf.Session()

with sess.as_default():

    for i in range(1,trials):

        for j in range(1,trials):

            if x.eval()**2 + y.eval()**2 <1:

                hits = hits+1

                pi.append((4*float(hits)/i)/trials)

plt.plot(pi)

plt.show()

출처: 책 텐서플로 입문 _ 잔카를로 자코네

반응형