CoCalc Public FilesCost function .sagewsOpen with one click!
Author: 희동 윤
Views : 131
Compute Environment: Ubuntu 18.04 (Deprecated)
import tensorflow as tf ##TensorFlow 함수 호출 import numpy as np ##numpy 함수 호출 num_points = 1000 ## 1000개의 데이터를 사용한다고 선언 vectors_set = [] ## 벡터 집합을 선언 for i in xrange(num_points): ## 데이터 생성 x1 = np.random.normal(0.0, 0.55) y1 = x1*0.1 + 0.5 + np.random.normal(0.0, 0.03) ## 직선 형태로 나오게 하기 위해, x1과 y1에 장난질 vectors_set.append([x1,y1]) x_data = [v[0] for v in vectors_set] ## 벡터의 첫번째 원소만 추출 y_data = [v[1] for v in vectors_set] ## 벡터의 두번째 원소만 추출
import matplotlib.pyplot as plt plt.plot(x_data,y_data,'ro') ## plt.legend() ## plt.show()
[<matplotlib.lines.Line2D object at 0x7f2af2e8e6d0>]
W = tf.Variable(tf.random_uniform([1],-1.0,1.0)) b = tf.Variable(tf.zeros([1])) y = W* x_data + b loss = tf.reduce_mean(tf.square(y-y_data)) optimizer = tf.train.GradientDescentOptimizer(0.5) ##알고리즘 명임. GradientDescent 알고리즘을 통해 a값을 Optimizer해라는 train = optimizer.minimize(loss) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init)
Tensor("Variable/read:0", shape=(1,), dtype=float32) Tensor("Variable_1/read:0", shape=(1,), dtype=float32)
for step in xrange(100): ## 머신 러닝 시작 sess.run(train) if step<10: ## 처음 10개의 변화는 급격하게 변화므로, 다 찍어 주고 print "step",step,":",sess.run(W), sess.run(b) else: if mod(step,10)==1: ## 이후에는 10회에 한번씩 찍어 주고 print "step",step,":",sess.run(W), sess.run(b) print "Last step:",sess.run(W), sess.run(b) ## 최종 적으로 얻은 함수 찍어 주고
step 0 : [-0.45349374] [ 0.50157368] step 1 : [-0.28082761] [ 0.5013684] step 2 : [-0.16229025] [ 0.50122786] step 3 : [-0.08091287] [ 0.50113142] step 4 : [-0.02504633] [ 0.50106519] step 5 : [ 0.01330673] [ 0.50101972] step 6 : [ 0.03963656] [ 0.50098848] step 7 : [ 0.05771231] [ 0.50096709] step 8 : [ 0.07012153] [ 0.50095236] step 9 : [ 0.07864062] [ 0.50094223] step 11 : [ 0.08850409] [ 0.50093055] step 21 : [ 0.09709222] [ 0.50092036] step 31 : [ 0.09729192] [ 0.50092012] step 41 : [ 0.09729657] [ 0.50092012] step 51 : [ 0.09729666] [ 0.50092012] step 61 : [ 0.09729666] [ 0.50092012] step 71 : [ 0.09729666] [ 0.50092012] step 81 : [ 0.09729666] [ 0.50092012] step 91 : [ 0.09729666] [ 0.50092012] Last step: [ 0.09729666] [ 0.50092012]
plt.plot(x_data,y_data,'ro') ## plt.plot(x_data,sess.run(W)*x_data+sess.run(b)) ## plt.legend() plt.show()
[<matplotlib.lines.Line2D object at 0x7fc32c604310>] [<matplotlib.lines.Line2D object at 0x7fc32c3226d0>]
@interact def _(A=matrix(RDF,3,3,[1/sqrt(2),-1/sqrt(2),0,1/sqrt(2),1/sqrt(2),0,0,0,2]),auto_update=false): D=A.det() E=abs(D) F=D/E Basis=[] for i in range(0,2): for j in range(0,2): for k in range(0,2): Basis += [vector(QQ,[i,j,k])] c=[] for i in range(0,8): c +=[A*Basis[i]] P = polygon3d([Basis[0],Basis[2],Basis[6],Basis[4]], opacity=0.5, color="red") P += polygon3d([Basis[1],Basis[3],Basis[7],Basis[5]], opacity=0.5, color="red") P += polygon3d([c[1],c[3],c[7],c[5]], opacity=0.5, color="blue") P += polygon3d([c[0],c[2],c[6],c[4]], opacity=0.5, color="blue") seq=[[0,1],[3,2],[6,7],[5,4],[0,1]] for i in range(0,4): P += polygon3d([Basis[seq[i][0]],Basis[seq[i][1]],Basis[seq[i+1][0]],Basis[seq[i+1][1]]], opacity=0.5, color="red") P += polygon3d([c[seq[i][0]],c[seq[i][1]],c[seq[i+1][0]],c[seq[i+1][1]]], opacity=0.5, color="blue") seq=[0,1,2,4] seq1=[0,'yellow','green','black'] for i in range(1,4): P += arrow3d((0,0,0),Basis[seq[i]],color=seq1[i]) P += arrow3d((0,0,0),c[seq[i]],color=seq1[i]) if F>0: html("&lt;Right direction&gt;") else: html("&lt;Left direction&gt;") print 'volume=',E P.show(spin=true)
Interact: please open in CoCalc