<jupyter_start><jupyter_text>**Bài 6 của mô hình hóa toán học (Sử dụng tensor flow 1.9.0)**<jupyter_code>pip install tensorflow==1.9.0
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np


#-------------------- [KHOI TAO] -------------------------#
# Nhap so diem du lieu:
N = 100

#Nhap step muon train vao dayyyy:
trainStep = 10000

# Nhap so node cua hidden layer muon train:
n_node = 100

# Nhap pt y' - f(x,y)
def func(dy_dx, x, y):
  lq = (1+3*(x**2))/(1+x+x**3)
  return dy_dx + (x + lq)*y - x**3 - 2*x - lq*x*x

# Nhap dieu kien ban dau y(x_k) = y_k
k = 0
y_k = 1
#----------------------------------------------------------#

#Tao N diem trong doan [0,2] (Train data)
x_train = np.linspace(0,2,N,endpoint=True)    
# Copy x_train vao x_t de tinh toan
x_t      = np.zeros((len(x_train),1))
x_t[:,0] = x_train


#Nghiem chinh xac de so sanh: y = e^(-0.5.x^2)/(1 + x + x^3) + x^2
def func_y(x):
  return np.exp(-0.5*x**2)/(1+x+x**3)+x**2

# Target data
y_train = func_y(x_train)


#------------------------ [KHOI TAO NEURAL NETWORK] ----------------------#
# khoi tao Input [N,1]
x1 = tf.placeholder("float", [None,1 ]) 

# hidden layer: Sigmoid (n_node units)
W1 = tf.Variable(tf.zeros([1, n_node])) 
b1 = tf.Variable(tf.zeros([n_node]))
# Output cua hidden layer: y1 = sigmoid(x1.w + b) [N, n_node]
y1 = tf.nn.sigmoid(tf.matmul(x1, W1)+b1)

# Output layer: linear (y1.W1) (1 units)
W2 = tf.Variable(tf.zeros([n_node, 1]))

# Network output [N,1]
y = tf.matmul(y1, W2)

# Tinh dao ham dy/dx
dif = tf.matmul(tf.multiply(y1*(1-y1),W1),W2) 
#---------------------------------------------------------------------------#

t_loss = (func(dif, x1, y))**2


# Tinh loss function: (y' - f(x,y))^2 + (y(x0) - y0)^2
loss = tf.reduce_mean(t_loss) + (y[k] - y_k)**2

# Optimize
train_step = tf.train.AdamOptimizer(1e-2).minimize(loss)
init = tf.global_variables_initializer()
sess = tf.InteractiveSession()
sess.run(init)

# Bat dau training  !!!
for i in range(trainStep):  
    sess.run(train_step,feed_dict={x1: x_t})
    if i%50 == 0:
        total_loss = sess.run(loss,feed_dict={x1: x_t})
        #In loss va in y0 de theo doi nha !!!
        print("Step {}:".format(i))
        print("loss = {}".format(total_loss))
        print("y0 = {}".format(sess.run(y[k], feed_dict={x1: x_t})))
        print("--------------------------------------------------")

# Luu model !!!
saver = tf.train.Saver(max_to_keep=1)
saver.save(sess,'Model cua Nhom/Phong.ckpt',global_step=trainStep)
saver = tf.train.Saver(max_to_keep=1)

# Lay model ra xai
model_file="Model cua Nhom/Phong.ckpt-" + str(trainStep)
saver.restore(sess, model_file)

#Day la output !!!
output = sess.run(y,feed_dict={x1:x_t})

# Khoi tao y_output co chieu bang x_train
y_output = x_train.copy()

for i in range(len(x_train)):
    y_output[i] = output[i]

# Ve bieu do:
plt.plot(x_train,y_train, 'r')
plt.plot(x_train,y_output, 'b')

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.24730127576054955, 'fraction_numerical': 0.029440628066732092, 'mean_word_length': 2.3347873500545258, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '367791', 'license': 'MIT', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1308, 'n_words': 279, 'path': 'cau6.ipynb', 'repo_name': 'hyper-windy/mathematicalModellingAssignment', 'size': '3057'}