О’zbekiston respublikasi axborot texnologiyalari va kommunikatsiyalarini rivojlantirish vazirligi muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti


Download 148.57 Kb.
bet2/2
Sana17.06.2023
Hajmi148.57 Kb.
#1531425
1   2
package topshiriq2;

import java.util.Scanner;

public class Main3 {
public static double getLoss(double[] x, double[] y, double w) {
double loss = 0;
for (int i = 0; i < x.length; i++) {
loss += Math.pow(y[i] - x[i] * w, 2);
}
return loss / x.length;
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("N=");
int N = input.nextInt();
System.out.print("X=");
int X = input.nextInt();
double[] w = new double[N];
double[] Y = new double[X];
double[] B = new double[X];
double[] S = new double[X];
double[] r = new double[N];
double[] g = new double[N];
double[] x = {4, 8, 9};
double[] y = {8, 16, 18};
double a = 0.01;
w[0] = 3.6;
double k = 0;

{
int i = 1;


while (i < N) {
w[i] = w[i - 1] - a * 2 * (w[i - 1] * x[0] - y[0]) * x[0];
r[i - 1] = w[i - 1] * x[0] - y[0];
g[i - 1] = Math.pow(w[i - 1] * x[0] - y[0], 2);
w[i] = w[i - 1] - a * 2 * (w[i - 1] * x[1] - y[1]) * x[1];
r[i - 1] = w[i - 1] * x[1] - y[1];
g[i - 1] = Math.pow(w[i - 1] * x[1] - y[1], 2);
w[i] = w[i - 1] - a * 2 * (w[i - 1] * x[2] - y[2]) * x[2];
r[i - 1] = w[i - 1] * x[2] - y[2];
g[i - 1] = Math.pow(w[i - 1] * x[2] - y[2], 2);
if (0.0001 < r[i - 1] && r[i - 1] < 0.001) {
k = w[i - 1];
break;
}
i++;
}
}
{
int i = 4;
while (i < X) {
Y[i] = k * i;
System.out.println(Y[i]);
i++;
}
}
int i = 4;
while (i < X) {
B[i] = Math.pow(Math.E, Y[i]) / (Math.pow(Math.E, Y[i]) + 1);
S[i] = (B[i] >= 0.5) ? 1 : 0;
System.out.println(S[i]);
i++;
}

}
}








  1. Eng soda perseptron yordamida hisoblashlarni amalga oshirish

  2. package topshiriq2;

    import java.util.Arrays;

    public class Main5 {
    private double[] weights;
    private double bias;
    private final int n_iters;
    private final double learning_rate;

    public Main5(int n_iters, double learning_rate) {
    this.n_iters = n_iters;
    this.learning_rate = learning_rate;
    }

    private double activation(double x) {
    return x >= 0 ? 1 : 0;
    }

    public void fit(double[][] X, double[] y) {
    int n_samples = X.length;
    int n_features = X[0].length;

    this.weights = new double[n_features];
    this.bias = 0;

    for (int i = 0; i < this.n_iters; i++) {
    for (int j = 0; j < n_samples; j++) {
    double linear_output = 0;
    for (int k = 0; k < n_features; k++) {
    linear_output += this.weights[k] * X[j][k];
    }
    linear_output += this.bias;

    double y_predicted = activation(linear_output);

    double update = this.learning_rate * (y[j] - y_predicted);

    for (int k = 0; k < n_features; k++) {
    this.weights[k] += update * X[j][k];
    }
    this.bias += update;
    }
    }
    }

    public double[] predict(double[][] X) {
    double[] y_predicted = new double[X.length];
    for (int i = 0; i < X.length; i++) {
    double linear_output = 0;
    for (int j = 0; j < this.weights.length; j++) {
    linear_output += this.weights[j] * X[i][j];
    }
    linear_output += this.bias;

    y_predicted[i] = activation(linear_output);


    }
    return y_predicted;
    }

    public static void main(String[] args) {
    double[][] X = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
    double[] y = {0, 0, 0, 1};

    Main5 perceptron = new Main5(1000, 0.01);


    perceptron.fit(X, y);

    double[][] test_data = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
    double[] predictions = perceptron.predict(test_data);
    System.out.println(Arrays.toString(predictions));
    }
    }



Download 148.57 Kb.

Do'stlaringiz bilan baham:
1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling