ข้ามไปที่เนื้อหาหลัก

เรื่องที่แนะนำ

คู่มือ Java ฉบับทันสมัย (อัปเดตล่าสุดปี 2025)

 คู่มือ Java ฉบับทันสมัย (อัปเดตล่าสุดปี 2025) บทนำ Java ยังคงเป็นภาษายอดนิยมที่ได้รับความนิยมอย่างต่อเนื่องตั้งแต่ยุค 1990 จนถึงปัจจุบัน ในปี 2025 Java ได้ก้าวหน้าอย่างมากทั้งด้านภาษาหลักและเครื่องมือสนับสนุน ล่าสุด Java 24 ได้เปิดตัวออกมาแล้ว และ Java 21 เป็นเวอร์ชัน LTS (Long-Term Support) ที่แนะนำให้ใช้งานสำหรับระบบจริงในระยะยาว บทความนี้จะช่วยให้คุณเข้าใจภาพรวมของ Java เวอร์ชันล่าสุด รวมถึงฟีเจอร์เด่น โครงสร้างภาษาที่ควรรู้ และแนวทางการพัฒนา Java ยุคใหม่ ภาพรวมเวอร์ชัน Java ล่าสุด เวอร์ชัน สถานะ วันเปิดตัว Java 24 เวอร์ชันล่าสุด มีนาคม 2025 Java 21 เวอร์ชัน LTS ล่าสุด กันยายน 2023 Java 17 LTS รุ่นก่อนหน้า กันยายน 2021 Java 11 LTS เก่า กันยายน 2018 โครงสร้างพื้นฐานของภาษา Java คลาสและอ็อบเจกต์ : โครงสร้างหลักของโปรแกรม Java Primitive Types : int, double, char, boolean เป็นต้น Control Statements : if, switch, while, for, do-while Methods : การแยกโค้ดเป็นหน่วยที่นำกลับมาใช้ซ้ำได้ Array และ Collection : จัดเก็บและจัดการข้อมูลหลายรายการ Exception Handling : try-catch-finally OOP Co...

สอนการใช้ Java Neuroph Auto Encoder แทรก Prompt เพื่อช่วยสร้างผลลัพท์

ตัวอย่างการใช้งาน Java Neuroph ด้วยการประยุกต์แทรก Prompt ไปผสมกับ Latent Vector เพื่อใช้เป็นตัวกำหนดผลลัพท์ร่วมนะครับ โดยยกตัวอย่าง Prompt สำหรับงาน Graphic เช่น GrayScale และ Sepia

import org.neuroph.core.Layer;
import org.neuroph.core.Neuron;
import org.neuroph.core.input.WeightedSum;
import org.neuroph.nnet.comp.neuron.InputNeuron;
import org.neuroph.nnet.comp.layer.InputLayer;
import org.neuroph.nnet.comp.layer.FullConnectedLayer;

import java.util.Arrays;

public class AutoencoderWithMiddlePrompt {

    public static void main(String[] args) {
        // Step 1: Define input and prompt layers
        InputLayer inputLayer = new InputLayer(4); // Example: 4 features for the main input
        InputLayer promptLayer = new InputLayer(2); // Example: 2 features for the prompt

        // Step 2: Encoder layer
        FullConnectedLayer encoderLayer = new FullConnectedLayer(3); // Latent representation

        // Step 3: Middle layer with combined latent + prompt
        FullConnectedLayer middleLayer = new FullConnectedLayer(5); // 3 latent + 2 prompt

        // Step 4: Decoder layer
        FullConnectedLayer decoderLayer = new FullConnectedLayer(4); // Reconstructed output

        // Step 5: Build connections between layers
        connectLayers(inputLayer, encoderLayer);      // Input to Encoder
        connectLayers(encoderLayer, middleLayer);     // Encoder to Middle Layer
        connectLayers(promptLayer, middleLayer);      // Prompt to Middle Layer
        connectLayers(middleLayer, decoderLayer);     // Middle Layer to Decoder

        // Step 6: Build the network
        NeuralNetwork autoencoder = new NeuralNetwork();
        autoencoder.addLayer(inputLayer);
        autoencoder.addLayer(promptLayer);
        autoencoder.addLayer(encoderLayer);
        autoencoder.addLayer(middleLayer);
        autoencoder.addLayer(decoderLayer);
        autoencoder.setInputNeurons(inputLayer.getNeurons());
        autoencoder.setOutputNeurons(decoderLayer.getNeurons());

        // Step 7: Training data
        double[][] trainingData = {
            {0.0, 1.0, 0.0, 1.0}, // Example input feature vectors
            {1.0, 0.0, 1.0, 0.0}
        };
        double[][] promptData = {
            {1.0, 0.0}, // Example prompt
            {0.0, 1.0}  // Example prompt
        };

        // Step 8: Training loop
        for (int epoch = 0; epoch < 1000; epoch++) {
            for (int i = 0; i < trainingData.length; i++) {
                double[] input = trainingData[i];
                double[] prompt = promptData[i];

                // Use prompt in the middle layer
                autoencoder.setInput(input);
                autoencoder.calculate();
                double[] latent = autoencoder.getLayerAt(encoderLayer.getIndex()).getNeuronsOutput();
                double[] combinedLatent = combineVectors(latent, prompt);

                // Use combined latent as input to the middle layer
                autoencoder.setLayerInput(middleLayer, combinedLatent);
                autoencoder.calculate();
                autoencoder.learn(input); // Learn to reconstruct the input
            }
        }

        // Step 9: Test the network with different prompts
        double[] testInput = {1.0, 0.0, 1.0, 0.0};
        double[] grayscalePrompt = {1.0, 0.0};
        double[] sepiaPrompt = {0.0, 1.0};

        System.out.println("Testing with Grayscale Prompt:");
        testWithPrompt(autoencoder, testInput, grayscalePrompt);

        System.out.println("Testing with Sepia Prompt:");
        testWithPrompt(autoencoder, testInput, sepiaPrompt);
    }

    // Helper method: Connect two layers
    private static void connectLayers(Layer from, Layer to) {
        for (Neuron fromNeuron : from.getNeurons()) {
            for (Neuron toNeuron : to.getNeurons()) {
                toNeuron.addInputConnection(fromNeuron);
            }
        }
    }

    // Helper method: Combine two vectors
    private static double[] combineVectors(double[] vector1, double[] vector2) {
        double[] combined = new double[vector1.length + vector2.length];
        System.arraycopy(vector1, 0, combined, 0, vector1.length);
        System.arraycopy(vector2, 0, combined, vector1.length, vector2.length);
        return combined;
    }

    // Helper method: Test the autoencoder with a given prompt
    private static void testWithPrompt(NeuralNetwork autoencoder, double[] input, double[] prompt) {
        autoencoder.setInput(input);
        autoencoder.calculate();
        double[] latent = autoencoder.getLayerAt(1).getNeuronsOutput(); // Get latent representation
        double[] combinedLatent = combineVectors(latent, prompt);

        autoencoder.setLayerInput(2, combinedLatent); // Feed combined latent to middle layer
        autoencoder.calculate();

        System.out.println("Reconstructed Output: " + Arrays.toString(autoencoder.getOutput()));
    }
}

ความคิดเห็น

บทความที่ได้รับความนิยม