import paradox as pd A = pd.Constant([[1, 2], [1, 3]], name='A') x = pd.Variable([0, 0], name='x') b = pd.Constant([3, 4], name='b') y = pd.reduce_sum((A @ x - b)**2) / 2 print('value =\n{}\n'.format(y.get_value())) # 完善自动求值 print('x gradient =\n{}\n'.format(y.get_gradient(x))) # 完善自动求导
c_y = c1_y + c2_y # 定义符号。 A = pd.Variable([c_x, c_y], name='A') W1 = pd.Variable(np.random.random((4, 2)), name='W1') # 输入层到隐含层的权重矩阵。 W2 = pd.Variable(np.random.random((2, 4)), name='W2') # 隐含层到输出层的权重矩阵。 B1 = pd.Variable(np.random.random((4, 1)), name='B1') # 隐含层的偏置。 B2 = pd.Variable(np.random.random((2, 1)), name='B2') # 输出层的偏置。 K = pd.Constant([[-1] * points_sum + [1] * points_sum, [1] * points_sum + [-1] * points_sum]) # 构建2x4x2网络,使用ReLu激活函数。 model = pd.maximum(W2 @ pd.maximum(W1 @ A + B1, 0) + B2, 0) # 使用SVM loss。 loss = pd.reduce_mean(pd.maximum(pd.reduce_sum(K * model, axis=0) + 1, 0)) # 创建loss计算引擎,申明变量为W1,W2,B1和B2。 loss_engine = pd.Engine(loss, [W1, W2, B1, B2]) # 创建梯度下降optimizer。 optimizer = pd.GradientDescentOptimizer(0.03) # 迭代至多10000次最小化loss。 for epoch in range(10000): optimizer.minimize(loss_engine) if epoch % 100 == 0: # 每100次epoch检查一次loss。 loss_value = loss_engine.value() print('loss = {:.8f}'.format(loss_value)) if loss_value < 0.001: # loss阈值。 break
import paradox as pd A = pd.Constant([[1, 2], [1, 3]], name='A') x = pd.Variable([0, 0], name='x') B = pd.Constant([3, 4], name='b') loss = pd.reduce_sum((A @ x - B)**2) / 2 e = pd.Engine(loss, x) print('loss formula =\n{}\n'.format(loss)) print('loss =\n{}\n'.format(e.value())) x_gradient = e.gradient(x) print('x gradient formula =\n{}\n'.format(x_gradient)) print('x gradient =\n{}\n'.format(pd.Engine(x_gradient).value()))
loss_engine = pd.Engine(loss, [W1, W2, W3, B1, B2, B3]) # 创建梯度下降optimizer。 optimizer = pd.GradientDescentOptimizer(0.001) # 迭代至多10000次最小化loss。 for epoch in range(10000): optimizer.minimize(loss_engine) if epoch % 100 == 0: # 每100次epoch检查一次loss。 loss_value = loss_engine.value() print('loss = {:.8f}'.format(loss_value)) if loss_value < 0.001: # loss阈值。 break # 创建预测函数。 predict = pd.where(pd.reduce_sum([[-1, 1]] * model, axis=1) < 0, -1, 1) # 创建预测函数计算引擎。 predict_engine = pd.Engine(predict) # 设置网格密度为0.1。 h = 0.1 # 生成预测采样点网格。 x, y = np.meshgrid(np.arange(np.min(c_x) - 1, np.max(c_x) + 1, h), np.arange(np.min(c_y) - 1, np.max(c_y) + 1, h)) # 绑定变量值。 predict_engine.bind = {A: np.array([x.ravel(), y.ravel()]).transpose()}
r = np.random.normal(4, 1) theta = np.random.normal(0, 2 * np.pi) c2_x.append(r * np.cos(theta)) c2_y.append(r * np.sin(theta)) c_x = c1_x + c2_x c_y = c1_y + c2_y A = pd.Constant([c_x, c_y], name='A') W1 = pd.Variable(np.random.random((4, 2)), name='W1') W2 = pd.Variable(np.random.random((2, 4)), name='W2') B1 = pd.Variable(np.random.random((4, 1)), name='B1') B2 = pd.Variable(np.random.random((2, 1)), name='B2') K = pd.Constant([[-1] * points_sum + [1] * points_sum, [1] * points_sum + [-1] * points_sum]) # 构建2x4x2网络,使用ReLu激活函数,SVM loss。 loss = pd.reduce_mean(pd.maximum(pd.reduce_sum(K * pd.maximum(W2 @ pd.maximum(W1 @ A + B1, 0) + B2, 0), axis=0) + 1, 0)) # 创建loss计算引擎,申明变量为W1,W2,B1和B2。 loss_engine = pd.Engine(loss, [W1, W2, B1, B2]) # 创建梯度下降optimizer。 optimizer = pd.GradientDescentOptimizer(0.0001) # 迭代至多10000次最小化loss。 for epoch in range(10000): optimizer.minimize(loss_engine) if epoch % 100 == 0: # 每100次epoch检查一次loss。 loss_value = loss_engine.value() print('loss = {:.8f}'.format(loss_value)) if loss_value < 0.001: # loss阈值。