akimova5
05.02.2020 02:57

с кодом Python from sklearnsklearn. import StandardScaler
from sklearn.model_selection import train_test_splittrain_test_split
from sklearn.metrics import accuracy_scoreaccuracy_score
import numpy as np
import numpy.random as r
import matplotlib.pyplot as plt

def convert_y_to_vect(y):
y_vect = np.zeros((len(y), 10))
for i in range(len(y)):
y_vect[i, y[i]] = 1
return y_vect

def f(x):
return 1 / (1 + np.exp(-x))

def f_deriv(x):
return f(x) * (1 - f(x))

def setup_and_init_weights(nn_structure):
W = {}
b = {}
for l in range(1, len(nn_structure)):
W[l] = r.random_sample((nn_structure[l], nn_structure[l-1]))
b[l] = r.random_sample((nn_structure[l],))
return W, b

def init_tri_values(nn_structure):
tri_W = {}
tri_b = {}
for l in range(1, len(nn_structure)):
tri_W[l] = np.zeros((nn_structure[l], nn_structure[l-1]))
tri_b[l] = np.zeros((nn_structure[l],))
return tri_W, tri_b

# Далее запустим процесс прямого распространения через нейронную сеть:
def feed_forward(x, W, b):
h = {1: x}
z = {}
#Если первый слой, то весами является x, в противном случае
#Это выход из последнего слоя
for l in range(1, len(W) + 1):
# if it is the first layer, then the input into the weights is x, otherwise,
# it is the output from the last layer
if l == 1:
node_in = x
else:
node_in = h[l]
z[l+1] = W[l].dot(node_in) + b[l] # z^(l+1) = W^(l)*h^(l) + b^(l)
h[l+1] = f(z[l+1]) # h^(l) = f(z^(l))
return h, z

def calculate_out_layer_delta(y, h_out, z_out):
# delta^(nl) = -(y_i - h_i^(nl)) * f'(z_i^(nl))
return -(y-h_out) * f_deriv(z_out)

def calculate_hidden_delta(delta_plus_1, w_l, z_l):
# delta^(l) = (transpose(W^(l)) * delta^(l+1)) * f'(z^(l))
return np.dot(np.transpose(w_l), delta_plus_1) * f_deriv(z_l)

def train_nn(nn_structure, X, y, iter_num=3000, alpha=0.25):
W, b = setup_and_init_weights(nn_structure)
cnt = 0
m = len(y)
avg_cost_func = []
print('Starting gradient descent for {} iterations'.format(iter_num))
while cnt < iter_num:
if cnt%1000 == 0:
print('Iteration {} of {}'.format(cnt, iter_num))
tri_W, tri_b = init_tri_values(nn_structure)
avg_cost = 0
for i in range(len(y)):
delta = {}
# perform the feed forward pass and return the stored h and z values, to be used in the
# gradient descent step
h, z = feed_forward(X[i, :], W, b)
# loop from nl-1 to 1 backpropagating the errors
for l in range(len(nn_structure), 0, -1):
if l == len(nn_structure):
delta[l] = calculate_out_layer_delta(y[i,:], h[l], z[l])
avg_cost += np.linalg.norm((y[i,:]-h[l]))
else:
if l >> 1:
delta[l] = calculate_hidden_delta(delta[l+1], W[l], z[l])
# triW^(l) = triW^(l) + delta^(l+1) * transpose(h^(l))
tri_W[l] += np.dot(delta[l+1][:,np.newaxis], np.transpose(h[l][:,np.newaxis]))
# trib^(l) = trib^(l) + delta^(l+1)
tri_b[l] += delta[l+1]
# perform the gradient descent step for the weights in each layer
# запускает градиентный спуск для весов в каждом слое
for l in range(len(nn_structure) - 1, 0, -1):
W[l] += -alpha * (1.0/m * tri_W[l])
b[l] += -alpha * (1.0/m * tri_b[l])
# завершает расчеты общей оценки
avg_cost = 1,0/m * avg_cost
avg_cost_func.append(avg_cost)
cnt += 1
return W, b, avg_cost_func
# return W, b, avg_cost_func = train_nn(nn_structure, X_train, y_v_train)
def predict_y(W, b, X, n_layers):
m = X.shape[0]
y = np.zeros((m,))
for i in range(m):
h, z = feed_forward(X[i, :], W, b)
y[i] = np.argmax(h[n_layers])
return y

if __name__ == "__main__":
# load data and scale
digits = load_digits()
X_scale = StandardScaler()
X = X_scale.fit_transform(digits.data)
y = digits.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4)
# convert digits to vectors
y_v_train = convert_y_to_vect(y_train)
y_v_test = convert_y_to_vect(y_test)
# setup the NN structure
nn_structure = [64, 30, 10]
# train the NN
W, b, avg_cost_func = train_nn(nn_structure, X_train, y_v_train)
# plot the avg_cost_func
plt.plot(avg_cost_func)
plt.ylabel('Average J')
plt.xlabel('Iteration number')
plt.show()
# get the prediction accuracy and print
y_pred = predict_y(W, b, X_test, 3)
print('Prediction accuracy is {}%'.format(accuracy_score(y_test, y_pred) * 100))

Нажмите на рекламу ниже и сразу увидите ответ
Популярные вопросы:
Ответ:
Tony271
11.05.2022 09:40
Блок-схема - в прилагаемом файле. Программа:
const n=20;
var a:array[1..20] of integer;
i,k2,k3,k4,k5:integer;
begin
Randomize;
writeln('Оценки:');
for i:=1 to n do begin
 a[i]:=random(4)+2;
 write(a[i],' ');
 end;
writeln;
k2:=0;k3:=0;k4:=0;k5:=0;
for i:=1 to n do
 case a[i] of
  2: k2:=k2+1;
  3: k3:=k3+1;
  4: k4:=k4+1;
  5: k5:=k5+1;
 end;
writeln('Двоек: ',k2);
writeln('Троек: ',k3);
writeln('Четверок: ',k4);
writeln('Пятёрок: ',k5);
end.

Пример:
Оценки:
3 5 3 2 5 3 2 5 3 4 4 5 2 3 3 4 4 2 2 5 
Двоек: 5
Троек: 6
Четверок: 4
Пятёрок: 5
Вклассе 20 учеников писали диктант по языку. напишите программу и начертите к ней блок-схему, подсчи
0,0(0 оценок)
Ответ:
larasargsian
13.02.2022 19:21
В приложении программы в файлах
1.
#include <iostream>
using namespace std;
int main()
{
    setlocale(LC_ALL,"Russian" );
    int k, n, sum = 0;
    cout << "Введите K: ";
    cin >> k;
    cout << "Введите N: ";
    cin >> n;

    for(int i = k ; i <= n; i++) {
        sum = sum + i;
    }

    cout << "Сумма чисел от " << k << " до " << n << " равна " << sum << endl;
    return 0;
}

2.
#include <iostream>
using namespace std;
int main()
{
    setlocale( LC_ALL,"Russian" );
    int k, n;
    cout << "Введите K: ";
    cin >> k;
    cout << "Введите N: ";
    cin >> n;

    for(int i = 1 ; i <= n; i++) {
        cout << k << endl;
    }
    return 0;
}

3.
#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i < 30; i++){
        if (!(i % 2 == 0)) {
            cout << i << endl;
        }
    }
    return 0;
}

4.
#include <iostream>
using namespace std;
int main()
{
    for(int i = 1; i <= 10; i++) {
        cout << i << " " << i*i << endl;
    }
    return 0;
}
0,0(0 оценок)
Полный доступ
Позволит учиться лучше и быстрее. Неограниченный доступ к базе и ответам от экспертов и ai-bota Оформи подписку
logo
Начни делиться знаниями
Вход Регистрация
Что ты хочешь узнать?
Спроси ai-бота