machine learning by 바죠


Chern number calculations and Topological phase transitions [model hamiltonian case] by 바죠

Chern number calculations and Topological phase transitions [model hamiltonian case]
2D band dispersion relations (2D BZ, two-band model)

Dirac hamiltonian
2D, broken time-reversal symmetry, two-band nontrivial structure, periodic in x and y directions, 2D BZ is torus,
the number of times the h(k)/|h|-vector wraps around the unit sphere.

topological invariants: nonlocal, nonlinear, intensive

h(kx,ky,m)=sin(kx) sigma1 + sin(ky) sigma2 +(m+cos(kx)+cos(ky)) sigma3

---------------------------------------------------------------------------------------------------------------------

# Written by In-Ho Lee, KRISS, May 12 (2019).
import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt

def genh(kx,ky,mm):
    h1=np.sin(kx)
    h2=np.sin(ky)
    h3=mm+np.cos(kx)+np.cos(ky)
    if False:
        tmp=h1**2+h2**2+h3**2
        if tmp < 1.e-13:
           tmp=0.
        tmp=np.sqrt(tmp)
        if tmp > 1.e-13:
            h1=h1/tmp
            h2=h2/tmp
            h3=h3/tmp
    return h1,h2,h3

def ndotvec(kx,ky,mm,ioption,delk):
    h1,h2,h3=genh(kx,ky,mm)
    kxp=kx+delk
    wec1,wec2,wec3=genh(kxp,ky,mm)
    if ioption == 1:
        dhdkx1=(wec1-h1)/delk
        dhdkx2=(wec2-h2)/delk
        dhdkx3=(wec3-h3)/delk
    if ioption == 0:
        kxp=kx-delk
        vec1,vec2,vec3=genh(kxp,ky,mm)
        dhdkx1=(wec1-vec1)/(2*delk)
        dhdkx2=(wec2-vec2)/(2*delk)
        dhdkx3=(wec3-vec3)/(2*delk)
    kyp=ky+delk
    wec1,wec2,wec3=genh(kx,kyp,mm)
    if ioption == 1:
        dhdky1=(wec1-h1)/delk
        dhdky2=(wec2-h2)/delk
        dhdky3=(wec3-h3)/delk
    if ioption == 0:
        kyp=ky-delk
        vec1,vec2,vec3=genh(kx,kyp,mm)
        dhdky1=(wec1-vec1)/(2*delk)
        dhdky2=(wec2-vec2)/(2*delk)
        dhdky3=(wec3-vec3)/(2*delk)
    if ioption == -1:
        dhdkx1=np.cos(kx)
        dhdkx2=0.
        dhdkx3=-np.sin(kx)
        dhdky1=0.
        dhdky2=np.cos(ky)
        dhdky3=-np.sin(ky)
    v1=dhdkx2*dhdky3-dhdkx3*dhdky2
    v2=dhdkx3*dhdky1-dhdkx1*dhdky3
    v3=dhdkx1*dhdky2-dhdkx2*dhdky1
    tmq=0.
    tmp=h1**2+h2**2+h3**2
    if tmp > 1.e-13:
       tmp=np.sqrt(tmp)
       tmp=tmp**3
       tmq=(h1*v1+h2*v2+h3*v3)/tmp
    return tmq


def defint(nx,ny,mm,ioption,delk):
    dx=2.*np.pi/float(nx-1)
    dy=2.*np.pi/float(ny-1)
    integral=0.
    for i in range(nx):
        kx=-np.pi+float(i)*dx
        for j in range(ny):
            ky=-np.pi+float(j)*dy
            integral=integral+ndotvec(kx,ky,mm,ioption,delk)
    return integral*(dx*dy)/(4.*np.pi)

def wvfromham(kx,ky,mm):
    s1=np.zeros((2,2),dtype=complex)
    s2=np.zeros((2,2),dtype=complex)
    s3=np.zeros((2,2),dtype=complex)
    s1[0,1]=1.
    s1[1,0]=1.
    s2[0,1]=-1j
    s2[1,0]=1j
    s3[0,0]=1
    s3[1,1]=-1
    ham=np.sin(kx)*s1+ np.sin(ky)*s2+ (mm+np.cos(kx)+ np.cos(ky))*s3
    assert(np.all(0 == (ham-np.conj(ham.T))))
    w,v=LA.eig(ham)
    idx=w.argsort()
    w=w[idx]
    v=v[:,idx]
    w=w.real
    if False:
        w,v=wvfromham(kx,ky,mm)
        for i in range(2):
            print(w[i])
            print(v[:,i])
        for i in range(2):
           for j in range(2):
               print( v[:,i]@v[:,j] )
    return w,v

mm=-4
mm=-3
mm=0
mm=-1
mm=1
mm=3
mm=4
print('m= ',mm)


nx=1001
ny=nx
delk=1.e-4
ioption=-1
integral=defint(nx,ny,mm,ioption,delk)
print(integral)


ng=200
shift=0.
x=[]
y1=[]
y2=[]
kvec1=np.zeros((2),dtype=float)
kvec2=np.zeros((2),dtype=float)
kvec1[0]=0.
kvec1[1]=0.
kvec2[0]=np.pi
kvec2[1]=0.
for i in range(ng):
    kx=kvec1[0]+float(i)*(kvec2[0]-kvec1[0])/float(ng-1)
    ky=kvec1[1]+float(i)*(kvec2[1]-kvec1[1])/float(ng-1)
    w,v=wvfromham(kx,ky,mm)
    tmp=np.sqrt((kx-kvec1[0])**2+(ky-kvec1[1])**2)
    x.append(tmp+shift)
    y1.append(w[0])
    y2.append(w[1])
x=np.asarray(x)
y1=np.asarray(y1)
y2=np.asarray(y2)
fig, ax=plt.subplots()
ax.plot(x,y1, 'o')
ax.plot(x,y2, '+')

shift=x[-1]
x=[]
y1=[]
y2=[]
kvec1[0]=np.pi
kvec1[1]=0.
kvec2[0]=np.pi
kvec2[1]=np.pi
for i in range(ng):
    kx=kvec1[0]+float(i)*(kvec2[0]-kvec1[0])/float(ng-1)
    ky=kvec1[1]+float(i)*(kvec2[1]-kvec1[1])/float(ng-1)
    tmp=np.sqrt((kx-kvec1[0])**2+(ky-kvec1[1])**2)
    w,v=wvfromham(kx,ky,mm)
    x.append(tmp+shift)
    y1.append(w[0])
    y2.append(w[1])
x=np.asarray(x)
y1=np.asarray(y1)
y2=np.asarray(y2)
ax.plot(x,y1, 'o')
ax.plot(x,y2, '+')

shift=x[-1]
x=[]
y1=[]
y2=[]
kvec1[0]=np.pi
kvec1[1]=np.pi
kvec2[0]=0.
kvec2[1]=0.
for i in range(ng):
    kx=kvec1[0]+float(i)*(kvec2[0]-kvec1[0])/float(ng-1)
    ky=kvec1[1]+float(i)*(kvec2[1]-kvec1[1])/float(ng-1)
    tmp=np.sqrt((kx-kvec1[0])**2+(ky-kvec1[1])**2)
    w,v=wvfromham(kx,ky,mm)
    x.append(tmp+shift)
    y1.append(w[0])
    y2.append(w[1])
x=np.asarray(x)
y1=np.asarray(y1)
y2=np.asarray(y2)
ax.plot(x,y1, 'o')
ax.plot(x,y2, '+')
#ax.set_title('')
fig.savefig("band.pdf")
plt.show()


---------------------------------------------------------------------------------------------------------------------

# Written by In-Ho Lee, KRISS, May 12 (2019).
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from numpy import linalg as LA

def genh(kx,ky,mm):
    h1=np.sin(kx)
    h2=np.sin(ky)
    h3=mm+np.cos(kx)+np.cos(ky)
    if False:
        tmp=h1**2+h2**2+h3**2
        if tmp < 1.e-13:
           tmp=0.
        tmp=np.sqrt(tmp)
        if tmp > 1.e-13:
            h1=h1/tmp
            h2=h2/tmp
            h3=h3/tmp
    return h1,h2,h3

def ndotvec(kx,ky,mm,ioption,delk):
    h1,h2,h3=genh(kx,ky,mm)
    kxp=kx+delk
    wec1,wec2,wec3=genh(kxp,ky,mm)
    if ioption == 1:
        dhdkx1=(wec1-h1)/delk
        dhdkx2=(wec2-h2)/delk
        dhdkx3=(wec3-h3)/delk
    if ioption == 0:
        kxp=kx-delk
        vec1,vec2,vec3=genh(kxp,ky,mm)
        dhdkx1=(wec1-vec1)/(2*delk)
        dhdkx2=(wec2-vec2)/(2*delk)
        dhdkx3=(wec3-vec3)/(2*delk)
    kyp=ky+delk
    wec1,wec2,wec3=genh(kx,kyp,mm)
    if ioption == 1:
        dhdky1=(wec1-h1)/delk
        dhdky2=(wec2-h2)/delk
        dhdky3=(wec3-h3)/delk
    if ioption == 0:
        kyp=ky-delk
        vec1,vec2,vec3=genh(kx,kyp,mm)
        dhdky1=(wec1-vec1)/(2*delk)
        dhdky2=(wec2-vec2)/(2*delk)
        dhdky3=(wec3-vec3)/(2*delk)
    if ioption == -1:
        dhdkx1=np.cos(kx)
        dhdkx2=0.
        dhdkx3=-np.sin(kx)
        dhdky1=0.
        dhdky2=np.cos(ky)
        dhdky3=-np.sin(ky)
    v1=dhdkx2*dhdky3-dhdkx3*dhdky2
    v2=dhdkx3*dhdky1-dhdkx1*dhdky3
    v3=dhdkx1*dhdky2-dhdkx2*dhdky1
    tmq=0.
    tmp=h1**2+h2**2+h3**2
    if tmp > 1.e-13:
       tmp=np.sqrt(tmp)
       tmp=tmp**3
    tmq=(h1*v1+h2*v2+h3*v3)/tmp
    return tmq

def defint(nx,ny,mm,ioption,delk):
    dx=2.*np.pi/float(nx-1)
    dy=2.*np.pi/float(ny-1)
    integral=0.
    for i in range(nx):
        kx=-np.pi+float(i)*dx
        for j in range(ny):
            ky=-np.pi+float(j)*dy
            integral=integral+ndotvec(kx,ky,mm,ioption,delk)
    return integral*(dx*dy)/(4.*np.pi)

def wvfromham(kx,ky,mm):
    s1=np.zeros((2,2),dtype=complex)
    s2=np.zeros((2,2),dtype=complex)
    s3=np.zeros((2,2),dtype=complex)
    s1[0,1]=1.
    s1[1,0]=1.
    s2[0,1]=-1j
    s2[1,0]=1j
    s3[0,0]=1
    s3[1,1]=-1
    ham=np.sin(kx)*s1+ np.sin(ky)*s2+ (mm+np.cos(kx)+ np.cos(ky))*s3
    assert(np.all(0 == (ham-np.conj(ham.T))))
    w,v=LA.eig(ham)
    idx=w.argsort()
    w=w[idx]
    v=v[:,idx]
    w=w.real
    if False:
        w,v=wvfromham(kx,ky,mm)
        for i in range(2):
            print(w[i])
            print(v[:,i])
        for i in range(2):
           for j in range(2):
               print( v[:,i]@v[:,j] )
    return w,v

mm=-4
mm=-3
mm=-1
mm=1
mm=3
mm=4
mm=0
print('m= ',mm)

nx=1001
ny=nx
delk=1.e-4
ioption=-1
integral=defint(nx,ny,mm,ioption,delk)
print(integral)

nx=51
ny=nx
x=np.zeros(nx)
y=np.zeros(ny)
Z=np.zeros((nx,ny))
W=np.zeros((nx,ny))
dx=2.*np.pi/float(nx-1)
dy=2.*np.pi/float(ny-1)
for i in range(nx):
     x[i]=-np.pi+float(i)*dx
for j in range(ny):
     y[j]=-np.pi+float(j)*dy

for i in range(nx):
     kx=x[i]
     for j in range(ny):
         ky=y[j]
         w,v= wvfromham(kx,ky,mm)
         Z[i,j]=w[0]
         W[i,j]=w[1]
X, Y =np.meshgrid(x,y)

fig = plt.figure()
ax=Axes3D(fig)
ax.plot_surface(X,Y,Z, rstride=1, cstride=1, cmap=cm.viridis)
ax.plot_surface(X,Y,W, rstride=1, cstride=1, cmap=cm.viridis)
#ax.set_title('')
fig.savefig("band3d.pdf")
plt.show()



---------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------

m     Chern number

-4       0

-3       0

-1       1

0       0

1      -1

3       0

4       0

m < -2,
-2< m < 0, 
0 < m < 2,  
m > 2
m 값에 따라서 동일한 Chern number를 가지는 구간이 있다.
그리고 m 값의 변화에 따라서 Chern number가 갑자기 바뀐다. 이를 topological phase transition이라고 한다.  
m_c=-2, 0, 2
topological phase transitions
이들 점에서는 사실 Chern number가 잘 정의가 안 된다. 왜냐하면, insulator가 아니기 때문이다.
밴드갭이 없어지기 전후를 비교해야 한다. 밴드갭이 없어지는 순간을 기준으로 Chern number의 변화가 발생한다.
정확하게 밴드갭이 없어지기 때문이다. 또한, 밴드갭이 없어지는 k point의 갯수가 다르다.
이 갯수는 Chern number 값의 변화와도 관계가 있다.
m=2 일 때, 4개의 k points에서 밴드갭이 0으로 간다.

---------------------------------------------------------------------------------------------------------------------
m=-2
---------------------------------------------------------------------------------------------------------------------

m=0
---------------------------------------------------------------------------------------------------------------------
# Written by In-Ho Lee, KRISS, May 12 (2019).
import numpy as np
from numpy import linalg as LA
import matplotlib.pyplot as plt

x=[]
y=[]
with open('output','r') as ifile:
     for line in ifile:
         for j in range(len(line.split())):
              if j == 0 :
                  m=int(line.split()[j])
                  x.append(m)
              if j == 1 :
                  c=float(line.split()[j])
                  y.append(c)
x=np.array(x)
y=np.array(y)

#fig, ax=plt.subplots()
fig, ax = plt.subplots(subplot_kw={'facecolor': "#ebf5ff"})
#ax.plot(x,y, 'o')
ax.scatter(x,y, s=25, c='r', marker='o', alpha=0.5)
ax.set_xlabel(r'$m$',fontsize=15)
ax.set_ylabel(r'$Chern(m)$',fontsize=15)
ax.set_xlim([-4, 4])
ax.set_ylim([-2, 2])
ax.grid(True)
fig.tight_layout()
#ax.set_title('')
fig.savefig("m_C.pdf")
plt.show()

---------------------------------------------------------------------------------------------------------------------




---------------------------------------------------------------------------------------------------------------------




pima, iris, housing [deep learning] by 바죠

Pima 인디언 당뇨병 문제

 

Training program

Prediction program 을 각각 나누어서 프로그램 한 예제는아래와 같다.

 

먼저 데이터가 아래와 같다고 가정할 경우 pima-indians-diabetes.csv

엑셀 파일과 같은 형식으로 comma separated value 양식일 때를 가정한다.

 

먼저 training 프로그램을 수행하면 완성된,학습된모델을 디스크에 저장할 수 있다. 아울러 가중치들도 모두 저장할 수 있다.

 

다음으로 새로운 데이터가 있다고 가정할 때, 물론, 지금의 연습에서는 동일한 데이터를 가정하고 있다.

Prediction 프로그램을 수행하면, 새로운 데이터를 읽어 들이고 이들 데이터에 대해서 예측을 수행할 수 있다.

이 경우, 순수한 예측으로서 정답이 알려져 있지 않다. X 벡터만있고, Y 벡터는 없다.

Y 벡터가 예측한 값이 될 것이다.

X를 읽어 들이고, 모델모양, 가중치를 모두 읽어 들인 다음, 예측을 수행한다. 이때 인풋은 X 벡터이다.

주어진 X 값에 대해서Y값이 예측된다.


http://incredible.egloos.com/7453361


https://machinelearningmastery.com/5-step-life-cycle-neural-network-models-keras/



--------------------------------------------------------------------------------------------------------------------
cat pima_training.py
# Binary classification
# A training program with keras
import time
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import random
import tensorflow as tf

tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()

# fix random seed for reproducibility
#np.random.seed(7)
# load pima indians dataset
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
X, Y = shuffle(X,Y,random_state=0)
x_train, x_test, y_train, y_test= train_test_split(X,Y, test_size=0.2)
# create model
model = Sequential()
model.add(Dense(12, input_dim=8, init='normal', activation='relu'))
for i in range(4):
    model.add(Dense(8, init='normal', activation='relu'))
model.add(Dense(1, init='normal', activation='sigmoid'))
model.summary()
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(x_train, y_train, validation_split=0.10, epochs=100, batch_size=5, verbose=2)
# evaluate the model
scores = model.evaluate(x_test, y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
predictions=model.predict(x_test)
rounded=[round(x[0]) for x in predictions]
print(rounded)

if True:
# serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
# serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")

print((time.clock()-start_time)/60./60.,'hours')



--------------------------------------------------------------------------------------------------------------------
 cat pima_prediction.py
# Binary classification
# A prediction program with keras
from keras.models import model_from_json
from sklearn.utils import shuffle
import time
import numpy as np
import random
import tensorflow as tf

tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()

# fix random seed for reproducibility
#np.random.seed(7)
# load pima indians dataset
dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
X, Y = shuffle(X,Y,random_state=0)

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='mean_squared_error', optimizer='adam')
predicted = loaded_model.predict(X)
print((time.clock()-start_time),'sec')

--------------------------------------------------------------------------------------------------------------------
cat iris_training.py
# Multi-class classification
# A training program with keras
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf

import time
import random
import numpy as np
import pandas as pd

tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()

df = pd.read_csv('iris.csv',names=["sepal_length", "sepal_width", "petal_length", "petal_width", "species"])
data_set = df.values
X = data_set[:, 0:4].astype(float)
obj_y = data_set[:, 4]

encoder = LabelEncoder()
encoder.fit(obj_y)
Y_encodered = encoder.transform(obj_y)
Y = np_utils.to_categorical(Y_encodered)

X, Y = shuffle(X,Y,random_state=0)
x_train, x_test, y_train, y_test= train_test_split(X,Y, test_size=0.2)

model = Sequential()
model.add(Dense(16, input_dim=4, activation='relu'))
for i in range(3):
    model.add(Dense(10, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, validation_split=0.10, epochs=100, batch_size=5, verbose=2)
scores=model.evaluate(x_test, y_test)
print('\nTest: Loss: {:.4f}'.format(scores[0]))
print('\nTest: Accuracy: {:.4f}'.format(scores[1]))
y_pred=model.predict(x_test)
y_pred=np.argmax(y_pred,axis=1)
y_test=np.argmax(y_test,axis=1)
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
if True:
# serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
# serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")
print((time.clock()-start_time)/60./60.,'hours')


--------------------------------------------------------------------------------------------------------------------
cat iris_prediction.py
# Multi-class classification
# A prediction program with keras
from keras.models import model_from_json
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf

import time
import random
import numpy as np
import pandas as pd

tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()

df = pd.read_csv('iris.csv',names=["sepal_length", "sepal_width", "petal_length", "petal_width", "species"])
data_set = df.values
X = data_set[:, 0:4].astype(float)
obj_y = data_set[:, 4]

encoder = LabelEncoder()
encoder.fit(obj_y)
Y_encodered = encoder.transform(obj_y)
Y = np_utils.to_categorical(Y_encodered)

X, Y = shuffle(X,Y,random_state=0)
x_train, x_test, y_train, y_test= train_test_split(X,Y, test_size=0.2)

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
# evaluate loaded model on test data
loaded_model.compile(loss='mean_squared_error', optimizer='adam')
predicted = loaded_model.predict(X)
print((time.clock()-start_time),'sec')



--------------------------------------------------------------------------------------------------------------------
cat hs_training.py
# Regression 
# A training program with keras
import time
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import random
import tensorflow as tf

tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()

df = pd.read_csv('housing.csv', delim_whitespace=True, header=None)

data_set = df.values
X = data_set[:, 0:13]
Y = data_set[:, 13]

X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=0.2)

model = Sequential()
model.add(Dense(30, input_dim=13, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(1))
model.summary()

model.compile(loss='mean_squared_error', optimizer='adam')
#model.fit(X_train, Y_train, epochs=200, batch_size=10)
model.fit(X_train, Y_train, validation_split=0.10, epochs=200, batch_size=10, verbose=2)

Y_prediction = model.predict(X_validation).flatten()
for i in range(10):
  real_price = Y_validation[i]
  predicted_price = Y_prediction[i]
  print('Real Price: {:.3f}, Predicted Price: {:.3f}'.format(real_price, predicted_price))


if True:
# serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
# serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")

print((time.clock()-start_time)/60./60.,'hours')

--------------------------------------------------------------------------------------------------------------------
cat hs_prediction.py
# Regression 
# A prediction program with keras
from keras.models import model_from_json
from sklearn.utils import shuffle
import time
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import random
import tensorflow as tf

tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()

df = pd.read_csv('housing.csv', delim_whitespace=True, header=None)
data_set = df.values
X = data_set[:, 0:13]
Y = data_set[:, 13]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

if True:
# load json and create model
    json_file = open('model.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
# load weights into new model
    loaded_model.load_weights("model.h5")
    print("Loaded model from disk")

loaded_model.compile(loss='mean_squared_error', optimizer='adam')
Y_prediction = loaded_model.predict(X_test).flatten()
for i in range(10):
  real_price = Y_test[i]
  predicted_price = Y_prediction[i]
  print('Real Price: {:.3f}, Predicted Price: {:.3f}'.format(real_price, predicted_price))
print((time.clock()-start_time)/60./60.,'hours')
--------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------


fortran unformatted file --> read in python by 바죠

fortran unformatted file --> read in python
최근 인공지능, 기계학습, 딥러닝, 빅 데이터 처리가 중요해 지고 있다.
IoT 시대에는 보다 많은 정보가 나타날 것으로 판단한다.
기계학습에서 다루는 데이터의 크기는 매우 큰 것들이다. 특히, 딥러닝의 경우는 더욱더 그렇다.
따라서, 데이터 처리는 정말로 특별한 장치가 필요하다. 바이너리 파일 취급이 필수적이다.
기본적으로 아스키 파일을 사용하면 곤란해질 수 있다.
많은 예제 데이터를 만들어 낼 경우, 컴퓨터 프로그래밍 언어 파이썬은 정답이 아닐 수 있다.
이 경우 포트란 또는 C를 활용해야 한다. 당연하게도 데이터는 바이너리 파일로 저장해야만 한다.
이렇게 저장된 바이너리 파일은 파이썬에서 바이너리 형식으로 읽어져야만 한다.
데이터 읽는 시간이 너무 많은 비중을 차지하게 된다.
이 부분을 효율적으로 처리하는 특단의 대책이 필요하다.
전체 파이썬 계산의 효율성을 극대화 할 수 있다.
다른 방식으로 파일 처러를 하면 안 된다.
예를 들어, 예제들의 갯수가 200000인 경우, 통상적인 파일로 읽어 드릴 경우, 매우 오랜 시간이 소요된다.

------------------------------------------------------------------------------------------------------
cat a.f90
!234567890
       implicit none
       integer i,j,nn
       real*8 rdata(10,10)

       nn=10
       rdata=0.d0
       do i=1,nn
       do j=1,10
       rdata(j,i)=i+0.0001d0*j
       enddo
       enddo
       open(11,file='fort.11',form='unformatted')
       write(11) nn
       do i=1,nn
       write(11) (rdata(j,i),j=1,10)
       enddo
       close(11)
       stop
       end


------------------------------------------------------------------------------------------------------
import numpy as np
from scipy.io import FortranFile
f=FortranFile('fort.11','r')
i=f.read_ints(dtype=np.int32)
print(i)
#print(i[0])
data=f.read_reals(dtype=np.float64)
print(data)
#print(data[0])
data=f.read_reals(dtype=np.float64)
#print(data[0])
print(data)

python a.py
[10]
[1.0001 1.0002 1.0003 1.0004 1.0005 1.0006 1.0007 1.0008 1.0009 1.001 ]
[2.0001 2.0002 2.0003 2.0004 2.0005 2.0006 2.0007 2.0008 2.0009 2.001 ]

------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------




1 2 3 4 5 6 7 8 9 10 다음

최근 포토로그