Skip to content

Commit 06be3d8

Browse files
committed
Initial commit
0 parents  commit 06be3d8

23 files changed

+1100
-0
lines changed

.gitignore

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import java.net.URL;
25+
import java.nio.channels.Channels;
26+
import java.nio.channels.ReadableByteChannel;
27+
import java.util.Properties;
28+
29+
public class MavenWrapperDownloader {
30+
31+
/**
32+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
33+
*/
34+
private static final String DEFAULT_DOWNLOAD_URL =
35+
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
36+
37+
/**
38+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
39+
* use instead of the default one.
40+
*/
41+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
42+
".mvn/wrapper/maven-wrapper.properties";
43+
44+
/**
45+
* Path where the maven-wrapper.jar will be saved to.
46+
*/
47+
private static final String MAVEN_WRAPPER_JAR_PATH =
48+
".mvn/wrapper/maven-wrapper.jar";
49+
50+
/**
51+
* Name of the property which should be used to override the default download url for the wrapper.
52+
*/
53+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
54+
55+
public static void main(String args[]) {
56+
System.out.println("- Downloader started");
57+
File baseDirectory = new File(args[0]);
58+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
59+
60+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
61+
// wrapperUrl parameter.
62+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
63+
String url = DEFAULT_DOWNLOAD_URL;
64+
if (mavenWrapperPropertyFile.exists()) {
65+
FileInputStream mavenWrapperPropertyFileInputStream = null;
66+
try {
67+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
68+
Properties mavenWrapperProperties = new Properties();
69+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
70+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
71+
} catch (IOException e) {
72+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
73+
} finally {
74+
try {
75+
if (mavenWrapperPropertyFileInputStream != null) {
76+
mavenWrapperPropertyFileInputStream.close();
77+
}
78+
} catch (IOException e) {
79+
// Ignore ...
80+
}
81+
}
82+
}
83+
System.out.println("- Downloading from: : " + url);
84+
85+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
86+
if (!outputFile.getParentFile().exists()) {
87+
if (!outputFile.getParentFile().mkdirs()) {
88+
System.out.println(
89+
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
90+
}
91+
}
92+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
93+
try {
94+
downloadFileFromURL(url, outputFile);
95+
System.out.println("Done");
96+
System.exit(0);
97+
} catch (Throwable e) {
98+
System.out.println("- Error downloading");
99+
e.printStackTrace();
100+
System.exit(1);
101+
}
102+
}
103+
104+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
105+
URL website = new URL(urlString);
106+
ReadableByteChannel rbc;
107+
rbc = Channels.newChannel(website.openStream());
108+
FileOutputStream fos = new FileOutputStream(destination);
109+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
110+
fos.close();
111+
rbc.close();
112+
}
113+
114+
}

.mvn/wrapper/maven-wrapper.jar

47.2 KB
Binary file not shown.

.mvn/wrapper/maven-wrapper.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip

demo01/init/t_example_data.sql

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Navicat Premium Data Transfer
3+
4+
Source Server : localhost
5+
Source Server Type : MySQL
6+
Source Server Version : 50636
7+
Source Host : localhost:3306
8+
Source Schema : test
9+
10+
Target Server Type : MySQL
11+
Target Server Version : 50636
12+
File Encoding : 65001
13+
14+
Date: 23/10/2019 16:15:09
15+
*/
16+
17+
SET NAMES utf8mb4;
18+
SET FOREIGN_KEY_CHECKS = 0;
19+
20+
-- ----------------------------
21+
-- Table structure for t_example_data
22+
-- ----------------------------
23+
DROP TABLE IF EXISTS `t_example_data`;
24+
CREATE TABLE `t_example_data` (
25+
`x` float(20,12) DEFAULT NULL,
26+
`y` float(20,12) DEFAULT NULL
27+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
28+
29+
SET FOREIGN_KEY_CHECKS = 1;

demo01/pom.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>deeplearning-java</artifactId>
7+
<groupId>com.codingapi.deepleaning</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>demo01</artifactId>
13+
14+
15+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.deeplearning.demo01;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
@SpringBootApplication
8+
@MapperScan(basePackages = "com.codingapi.deeplearning.demo01.mapper")
9+
public class DeepLearningJavaDemo01Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(DeepLearningJavaDemo01Application.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.codingapi.deeplearning.demo01.db;
2+
3+
import com.codingapi.deeplearning.demo01.domian.ExampleData;
4+
import com.codingapi.deeplearning.demo01.learn.ExampleDataArrays;
5+
import com.codingapi.deeplearning.demo01.learn.RegressionFunction;
6+
import com.codingapi.deeplearning.demo01.mapper.ExampleDataMapper;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.math.BigDecimal;
11+
import java.util.List;
12+
13+
/**
14+
* @author lorne
15+
* @date 2019-10-22
16+
* @description y = 1+5x
17+
*/
18+
@Component
19+
public class RegressionFunctionExampleData {
20+
21+
@Autowired
22+
private ExampleDataMapper exampleDataMapper;
23+
24+
25+
public void randData(int number,double a,double b){
26+
exampleDataMapper.tuncate();
27+
RegressionFunction regressionFunction = new RegressionFunction(a,b);
28+
for(int i=0;i<number;i++){
29+
//为什么将x/number ,是为了梯度下降可更高效的操作,而做的特征缩放
30+
BigDecimal x = new BigDecimal(i).divide(new BigDecimal(number),5,BigDecimal.ROUND_UP);
31+
BigDecimal y = regressionFunction.getY(x);
32+
exampleDataMapper.save(new ExampleData(x,y));
33+
}
34+
}
35+
36+
37+
public List<ExampleData> findAll(){
38+
return exampleDataMapper.findAll();
39+
}
40+
41+
42+
public ExampleDataArrays loadData(){
43+
List<ExampleData> exampleDataList = findAll();
44+
ExampleDataArrays exampleDataArrays = new ExampleDataArrays();
45+
int size = exampleDataList.size();
46+
double [] x = new double[size];
47+
double [] y = new double[size];
48+
for(int i=0;i<size;i++){
49+
ExampleData exampleData = exampleDataList.get(i);
50+
x[i] = exampleData.getX().doubleValue();
51+
y[i] = exampleData.getY().doubleValue();
52+
}
53+
exampleDataArrays.setX(x);
54+
exampleDataArrays.setY(y);
55+
return exampleDataArrays;
56+
}
57+
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.codingapi.deeplearning.demo01.domian;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.math.BigDecimal;
8+
9+
/**
10+
* @author lorne
11+
* @date 2019-10-22
12+
* @description
13+
*/
14+
@Data
15+
@AllArgsConstructor
16+
@NoArgsConstructor
17+
public class ExampleData {
18+
19+
private BigDecimal x;
20+
private BigDecimal y;
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.codingapi.deeplearning.demo01.learn;
2+
3+
/**
4+
* 代价函数
5+
* @author lorne
6+
* @date 2019-10-22
7+
* J(θ0,θ1) = 1/2m ∑... (对不起,实在打不出来,看图片吧)
8+
* 见:images/cost&hypothesis.jpeg
9+
*
10+
*/
11+
public class CostFunction {
12+
13+
//实际的Y值
14+
private double[] y;
15+
16+
//通过假如函数计算出来的Y值 y^
17+
private double[] yy;
18+
19+
//假如函数与实际值的初始化
20+
public CostFunction(double[] y, double[] yy) {
21+
this.y = y;
22+
this.yy = yy;
23+
}
24+
25+
public double getVal(){
26+
double sum = 0;
27+
28+
int number = y.length;
29+
30+
//计算差值的平方
31+
for(int i=0;i<number;i++){
32+
sum+=(yy[i]-y[i])*(yy[i]-y[i]);
33+
}
34+
35+
//计算平均值
36+
return sum/number/2;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.codingapi.deeplearning.demo01.learn;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* @author lorne
7+
* @date 2019-10-23
8+
* @description
9+
*/
10+
@Data
11+
public class ExampleDataArrays {
12+
13+
private double[] x;
14+
private double[] y;
15+
}

0 commit comments

Comments
 (0)