Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>DeeplearnJS logistic regresion demo</title>

    <!-- deeplearn.js -->
    <script src="https://unpkg.com/deeplearn@latest"></script>
    <!-- highcharts.js -->
    <script src="https://cdn.hcharts.cn/highcharts/highcharts.js"></script>
  
</head>
<body>
    <div id="app" style="width: 500px;height:400px;"></div>
    
    <script>
      (async function() {
        Highcharts.theme = {
   colors: ['#DF5353', '#90ee7e', '#f45b5b', '#7798BF', '#aaeeee', '#ff0066',
      '#eeaaee', '#55BF3B', '#DF5353', '#7798BF', '#aaeeee'],
   chart: {
      backgroundColor: {
         linearGradient: { x1: 0, y1: 0, x2: 1, y2: 1 },
         stops: [
            [0, '#2a2a2b'],
            [1, '#3e3e40']
         ]
      },
      style: {
         fontFamily: '\'Unica One\', sans-serif'
      },
      plotBorderColor: '#606063'
   },
   title: {
      style: {
         color: '#E0E0E3',
         textTransform: 'uppercase',
         fontSize: '20px'
      }
   },
   subtitle: {
      style: {
         color: '#E0E0E3',
         textTransform: 'uppercase'
      }
   },
   xAxis: {
      gridLineColor: '#707073',
      labels: {
         style: {
            color: '#E0E0E3'
         }
      },
      lineColor: '#707073',
      minorGridLineColor: '#505053',
      tickColor: '#707073',
      title: {
         style: {
            color: '#A0A0A3'
         }
      }
   },
   yAxis: {
      gridLineColor: '#707073',
      labels: {
         style: {
            color: '#E0E0E3'
         }
      },
      lineColor: '#707073',
      minorGridLineColor: '#505053',
      tickColor: '#707073',
      tickWidth: 1,
      title: {
         style: {
            color: '#A0A0A3'
         }
      }
   },
   tooltip: {
      backgroundColor: 'rgba(0, 0, 0, 0.85)',
      style: {
         color: '#F0F0F0'
      }
   },
   
   legend: {
      itemStyle: {
         color: '#E0E0E3'
      },
      itemHoverStyle: {
         color: '#FFF'
      },
      itemHiddenStyle: {
         color: '#606063'
      }
   },
   credits: {
      style: {
         color: '#666'
      }
   },
   labels: {
      style: {
         color: '#707073'
      }
   }
};

// Apply the theme
Highcharts.setOptions(Highcharts.theme);


// 建立假資料,1 代表智商 100 分以上,0 代表智商 100 分以下
const data = [] 
for (let i=0; i<200; i++) {
    let tmpX1 = Math.random() * 120 + 60;
    let tmpX2 = Math.random() * 120 + 60;
    data.push({
        x: tmpX1,
        y: tmpX2,
        c: tmpX1 > 100 && tmpX2 > 100 ? 1: 0
    });
}

/**
 * deeplearn.js 運算
 */
const x_list = [];
const y_list = [];

for (let elem of data) {
    x_list.push([elem.x, elem.y]);
    y_list.push(elem.c);
}

const x_data = dl.tensor2d(x_list);
const y_data = dl.tensor2d(y_list);

// 權重與偏差
const W = dl.variable(dl.zeros([1, 2]));
const B = dl.variable(dl.zeros([1]));

// logistic regression 模型
const f = x => dl.sigmoid(W.matMul(x.transpose()).add(B));
// loss function(log loss)
const loss = (pred, label) => dl.mean(dl.neg(dl.sum(dl.mul(label, dl.log(pred)))));

// 梯度優化
const learningRate = 0.001;
const optimizer = dl.train.sgd(learningRate);

// Training!
for (let i = 0; i < 1000; i++) {
  const cost = optimizer.minimize(() => loss(f(x_data), y_data), true);
}


// 用 dataSync 取得 training 結果
const wPredict = W.dataSync();
const bPredict = B.dataSync();
console.log(wPredict, bPredict);

// 計算切割線段
const data_line = [
    [60, parseFloat((180 * wPredict[0] + bPredict[0])/wPredict[1])],
    [180, parseFloat((60 * wPredict[0] + bPredict[0])/wPredict[1])]
];
console.log(data_line);

        
// 繪製圖形
const data_scatter1 = [];
const data_scatter2 = [];
for (let elem of data) {
    if (elem.x > 100 && elem.y > 100) {
        data_scatter1.push([elem.x, elem.y]);
    } else {
        data_scatter2.push([elem.x, elem.y]);
    }  
}
        
// Result
const options = {
    title: {
        text: 'deeplearn.js 你是聰明人嗎?'                 
    },
    xAxis: {
        title: {
          text: '智商'                 
        },
        min: 60,
        max: 180
    },
    yAxis: {
        title: {
          text: '書讀得多寡'                 
        },
        min: 60,
        max: 180
    },
    series: [
        {
            type: 'line',
            name: 'Decision Boundary', 
            color: '#fff600',
            data: data_line
        },  
        {
            type: 'scatter',
            name: 'Smart', 
            marker: {
                symbol: 'cross',  
                radius: 4         
            },
            color: '#FF0000',
            data: data_scatter1
        },
        {
            type: 'scatter',
            name: 'Stupid',
            marker: {
                symbol: 'cross',  
                radius: 4         
            },
            color: '#6B8E23',
            data: data_scatter2
        }
    ]
};
// 图表初始化函数
const chart = Highcharts.chart('app', options);
      })();

    </script>
</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console