Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-15393
This commit is contained in:
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.bucketsort;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class IntegerBucketSorter implements Sorter<Integer> {
|
||||
|
||||
private final Comparator<Integer> comparator;
|
||||
|
||||
public IntegerBucketSorter(Comparator<Integer> comparator) {
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
public IntegerBucketSorter() {
|
||||
comparator = Comparator.naturalOrder();
|
||||
}
|
||||
|
||||
public List<Integer> sort(List<Integer> arrayToSort) {
|
||||
|
||||
List<List<Integer>> buckets = splitIntoUnsortedBuckets(arrayToSort);
|
||||
|
||||
for(List<Integer> bucket : buckets){
|
||||
bucket.sort(comparator);
|
||||
}
|
||||
|
||||
return concatenateSortedBuckets(buckets);
|
||||
}
|
||||
|
||||
private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
|
||||
List<Integer> sortedArray = new ArrayList<>();
|
||||
int index = 0;
|
||||
for(List<Integer> bucket : buckets){
|
||||
for(int number : bucket){
|
||||
sortedArray.add(index++, number);
|
||||
}
|
||||
}
|
||||
return sortedArray;
|
||||
}
|
||||
|
||||
private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){
|
||||
|
||||
final int[] codes = createHashes(initialList);
|
||||
|
||||
List<List<Integer>> buckets = new ArrayList<>(codes[1]);
|
||||
for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>());
|
||||
|
||||
//distribute the data
|
||||
for (int i : initialList) {
|
||||
buckets.get(hash(i, codes)).add(i);
|
||||
}
|
||||
return buckets;
|
||||
|
||||
}
|
||||
|
||||
private int[] createHashes(List<Integer> input){
|
||||
int m = input.get(0);
|
||||
for (int i = 1; i < input.size(); i++) {
|
||||
if (m < input.get(i)) {
|
||||
m = input.get(i);
|
||||
}
|
||||
}
|
||||
return new int[]{m, (int) Math.sqrt(input.size())};
|
||||
}
|
||||
|
||||
private static int hash(int i, int[] code) {
|
||||
return (int) ((double) i / code[0] * (code[1] - 1));
|
||||
}
|
||||
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.bucketsort;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Sorter<T> {
|
||||
|
||||
List<T> sort(List<T> arrayToSort);
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.bucketsort;
|
||||
|
||||
import com.baeldung.bucketsort.IntegerBucketSorter;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class IntegerBucketSorterUnitTest {
|
||||
|
||||
private IntegerBucketSorter sorter;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
sorter = new IntegerBucketSorter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() {
|
||||
|
||||
List<Integer> unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15);
|
||||
List<Integer> expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602);
|
||||
|
||||
List<Integer> actual = sorter.sort(unsorted);
|
||||
|
||||
assertEquals(expected, actual);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,11 @@
|
||||
<version>${spock-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.groovy-wslite</groupId>
|
||||
<artifactId>groovy-wslite</artifactId>
|
||||
<version>${groovy-wslite.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -175,6 +180,7 @@
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<groovy-wslite.version>1.1.3</groovy-wslite.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<logback.version>1.2.3</logback.version>
|
||||
<groovy.version>2.5.7</groovy.version>
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
package com.baeldung.webservice
|
||||
|
||||
import groovy.json.JsonSlurper
|
||||
import wslite.rest.ContentType
|
||||
import wslite.rest.RESTClient
|
||||
import wslite.rest.RESTClientException
|
||||
import wslite.soap.SOAPClient
|
||||
import wslite.soap.SOAPMessageBuilder
|
||||
import wslite.http.auth.HTTPBasicAuthorization
|
||||
import org.junit.Test
|
||||
|
||||
class WebserviceUnitTest extends GroovyTestCase {
|
||||
|
||||
JsonSlurper jsonSlurper = new JsonSlurper()
|
||||
|
||||
static RESTClient client = new RESTClient("https://postman-echo.com")
|
||||
|
||||
static {
|
||||
client.defaultAcceptHeader = ContentType.JSON
|
||||
client.httpClient.sslTrustAllCerts = true
|
||||
}
|
||||
|
||||
void test_whenSendingGet_thenRespose200() {
|
||||
def postmanGet = new URL('https://postman-echo.com/get')
|
||||
def getConnection = postmanGet.openConnection()
|
||||
getConnection.requestMethod = 'GET'
|
||||
assert getConnection.responseCode == 200
|
||||
if (getConnection.responseCode == 200) {
|
||||
assert jsonSlurper.parseText(getConnection.content.text)?.headers?.host == "postman-echo.com"
|
||||
}
|
||||
}
|
||||
|
||||
void test_whenSendingPost_thenRespose200() {
|
||||
def postmanPost = new URL('https://postman-echo.com/post')
|
||||
def query = "q=This is post request form parameter."
|
||||
def postConnection = postmanPost.openConnection()
|
||||
postConnection.requestMethod = 'POST'
|
||||
assert postConnection.responseCode == 200
|
||||
}
|
||||
|
||||
void test_whenSendingPostWithParams_thenRespose200() {
|
||||
def postmanPost = new URL('https://postman-echo.com/post')
|
||||
def form = "param1=This is request parameter."
|
||||
def postConnection = postmanPost.openConnection()
|
||||
postConnection.requestMethod = 'POST'
|
||||
postConnection.doOutput = true
|
||||
def text
|
||||
postConnection.with {
|
||||
outputStream.withWriter { outputStreamWriter ->
|
||||
outputStreamWriter << form
|
||||
}
|
||||
text = content.text
|
||||
}
|
||||
assert postConnection.responseCode == 200
|
||||
assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter."
|
||||
}
|
||||
|
||||
void test_whenReadingRSS_thenReceiveFeed() {
|
||||
def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en")
|
||||
def stories = []
|
||||
(0..4).each {
|
||||
def item = rssFeed.channel.item.get(it)
|
||||
stories << item.title.text()
|
||||
}
|
||||
assert stories.size() == 5
|
||||
}
|
||||
|
||||
void test_whenReadingAtom_thenReceiveFeed() {
|
||||
def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en")
|
||||
def stories = []
|
||||
(0..4).each {
|
||||
def entry = atomFeed.entry.get(it)
|
||||
stories << entry.title.text()
|
||||
}
|
||||
assert stories.size() == 5
|
||||
}
|
||||
|
||||
void test_whenConsumingSoap_thenReceiveResponse() {
|
||||
def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso"
|
||||
def soapClient = new SOAPClient(url)
|
||||
def message = new SOAPMessageBuilder().build({
|
||||
body {
|
||||
NumberToWords(xmlns: "http://www.dataaccess.com/webservicesserver/") {
|
||||
ubiNum(1234)
|
||||
}
|
||||
}
|
||||
})
|
||||
def response = soapClient.send(message.toString());
|
||||
def words = response.NumberToWordsResponse
|
||||
assert words == "one thousand two hundred and thirty four "
|
||||
}
|
||||
|
||||
void test_whenConsumingRestGet_thenReceiveResponse() {
|
||||
def path = "/get"
|
||||
def response
|
||||
try {
|
||||
response = client.get(path: path)
|
||||
assert response.statusCode == 200
|
||||
assert response.json?.headers?.host == "postman-echo.com"
|
||||
} catch (RESTClientException e) {
|
||||
assert e?.response?.statusCode != 200
|
||||
}
|
||||
}
|
||||
|
||||
void test_whenConsumingRestPost_thenReceiveResponse() {
|
||||
def path = "/post"
|
||||
def params = ["foo":1,"bar":2]
|
||||
def response
|
||||
try {
|
||||
response = client.post(path: path) {
|
||||
type ContentType.JSON
|
||||
json params
|
||||
}
|
||||
assert response.json?.data == params
|
||||
} catch (RESTClientException e) {
|
||||
e.printStackTrace()
|
||||
assert e?.response?.statusCode != 200
|
||||
}
|
||||
}
|
||||
|
||||
void test_whenBasicAuthentication_thenReceive200() {
|
||||
def path = "/basic-auth"
|
||||
def response
|
||||
try {
|
||||
client.authorization = new HTTPBasicAuthorization("postman", "password")
|
||||
response = client.get(path: path)
|
||||
assert response.statusCode == 200
|
||||
assert response.json?.authenticated == true
|
||||
} catch (RESTClientException e) {
|
||||
e.printStackTrace()
|
||||
assert e?.response?.statusCode != 200
|
||||
}
|
||||
}
|
||||
|
||||
void test_whenOAuth_thenReceive200() {
|
||||
RESTClient oAuthClient = new RESTClient("https://postman-echo.com")
|
||||
oAuthClient.defaultAcceptHeader = ContentType.JSON
|
||||
oAuthClient.httpClient.sslTrustAllCerts = true
|
||||
|
||||
def path = "/oauth1"
|
||||
def params = [oauth_consumer_key: "RKCGzna7bv9YD57c", oauth_signature_method: "HMAC-SHA1", oauth_timestamp:1567089944, oauth_nonce: "URT7v4", oauth_version: 1.0, oauth_signature: 'RGgR/ktDmclkM0ISWaFzebtlO0A=']
|
||||
def response
|
||||
try {
|
||||
response = oAuthClient.get(path: path, query: params)
|
||||
assert response.statusCode == 200
|
||||
assert response.statusMessage == "OK"
|
||||
assert response.json.status == "pass"
|
||||
} catch (RESTClientException e) {
|
||||
assert e?.response?.statusCode != 200
|
||||
}
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.java.list;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
public class RemoveFromList {
|
||||
|
||||
public static void main(String[] args) {
|
||||
List<String> sports = new ArrayList<>();
|
||||
sports.add("Football");
|
||||
sports.add("Basketball");
|
||||
sports.add("Baseball");
|
||||
sports.add("Boxing");
|
||||
sports.add("Cycling");
|
||||
|
||||
System.out.println("List before removing: " + sports);
|
||||
|
||||
// Remove with index
|
||||
sports.remove(1);
|
||||
|
||||
// Remove with an element
|
||||
sports.remove("Baseball");
|
||||
|
||||
// Iterator remove method
|
||||
Iterator<String> iterator = sports.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (iterator.next().equals("Boxing")) {
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayList removeIf method (Java 8)
|
||||
sports.removeIf(p -> p.equals("Cycling"));
|
||||
|
||||
System.out.println("List after removing: " + sports);
|
||||
}
|
||||
|
||||
}
|
||||
+2
-14
@@ -17,15 +17,9 @@ public class FileReaderExampleUnitTest {
|
||||
public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException {
|
||||
String expectedText = "Hello, World!";
|
||||
File file = new File(FILE_PATH);
|
||||
FileReader fileReader = null;
|
||||
try {
|
||||
fileReader = new FileReader(file);
|
||||
try (FileReader fileReader = new FileReader(file)) {
|
||||
String content = FileReaderExample.readAllCharactersOneByOne(fileReader);
|
||||
Assert.assertEquals(expectedText, content);
|
||||
} finally {
|
||||
if (fileReader != null) {
|
||||
fileReader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,15 +27,9 @@ public class FileReaderExampleUnitTest {
|
||||
public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException {
|
||||
String expectedText = "Hello";
|
||||
File file = new File(FILE_PATH);
|
||||
FileReader fileReader = null;
|
||||
try {
|
||||
fileReader = new FileReader(file);
|
||||
try (FileReader fileReader = new FileReader(file)) {
|
||||
String content = FileReaderExample.readMultipleCharacters(fileReader, 5);
|
||||
Assert.assertEquals(expectedText, content);
|
||||
} finally {
|
||||
if (fileReader != null) {
|
||||
fileReader.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -207,6 +207,21 @@
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<!-- Build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.resource.MyResourceLoader</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
@@ -274,6 +289,8 @@
|
||||
<!-- Mime Type Libraries -->
|
||||
<tika.version>1.18</tika.version>
|
||||
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.resource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MyResourceLoader {
|
||||
|
||||
private void loadFileWithReader() throws IOException {
|
||||
|
||||
try (FileReader fileReader = new FileReader("src/main/resources/input.txt");
|
||||
BufferedReader reader = new BufferedReader(fileReader)) {
|
||||
String contents = reader.lines()
|
||||
.collect(Collectors.joining(System.lineSeparator()));
|
||||
System.out.println(contents);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void loadFileAsResource() throws IOException {
|
||||
|
||||
try (InputStream inputStream = getClass().getResourceAsStream("/input.txt");
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String contents = reader.lines()
|
||||
.collect(Collectors.joining(System.lineSeparator()));
|
||||
System.out.println(contents);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
MyResourceLoader resourceLoader = new MyResourceLoader();
|
||||
|
||||
resourceLoader.loadFileAsResource();
|
||||
resourceLoader.loadFileWithReader();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Employee {
|
||||
|
||||
protected int id;
|
||||
protected String name;
|
||||
protected Date startDate;
|
||||
|
||||
public Employee(int id, String name, Date startDate) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public Employee(Employee employee) {
|
||||
this.id = employee.id;
|
||||
this.name = employee.name;
|
||||
this.startDate = new Date(employee.startDate.getTime());
|
||||
}
|
||||
|
||||
Date getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public Employee copy() {
|
||||
return new Employee(this);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Manager extends Employee {
|
||||
|
||||
private List<Employee> directReports;
|
||||
|
||||
public Manager(int id, String name, Date startDate, List<Employee> directReports) {
|
||||
super(id, name, startDate);
|
||||
this.directReports = directReports;
|
||||
}
|
||||
|
||||
public Manager(Manager manager) {
|
||||
super(manager.id, manager.name, manager.startDate);
|
||||
this.directReports = manager.directReports.stream()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Employee copy() {
|
||||
return new Manager(this);
|
||||
}
|
||||
|
||||
List<Employee> getDirectReport() {
|
||||
return this.directReports;
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class EmployeeUnitTest {
|
||||
@Test
|
||||
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
|
||||
Date d1 = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", d1);
|
||||
Employee e2 = new Employee(e1);
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertEquals(d1, e2.getStartDate());
|
||||
|
||||
d1.setTime(456);
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertNotEquals(d1, e2.getStartDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCopyMethod_whenCopy_thenDistinct() {
|
||||
Date d1 = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", d1);
|
||||
Employee e2 = e1.copy();
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertEquals(d1, e2.getStartDate());
|
||||
|
||||
d1.setTime(456);
|
||||
assertEquals(d1, e1.getStartDate());
|
||||
assertNotEquals(d1, e2.getStartDate());
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.copyconstructor;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ManagerUnitTest {
|
||||
@Test
|
||||
public void givenCopyConstructor_whenDeepCopy_thenDistinct() {
|
||||
Date startDate = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", startDate);
|
||||
Employee e2 = new Employee(e1);
|
||||
List<Employee> directReports = new ArrayList<Employee>();
|
||||
directReports.add(e1);
|
||||
directReports.add(e2);
|
||||
|
||||
Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports);
|
||||
Manager m2 = new Manager(m1);
|
||||
List<Employee> directReports1 = m1.getDirectReport();
|
||||
List<Employee> directReports2 = m2.getDirectReport();
|
||||
assertEquals(directReports1.size(), directReports2.size());
|
||||
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
|
||||
|
||||
// clear m1's direct reports list. m2's list should not be affected
|
||||
directReports.clear();
|
||||
directReports1 = m1.getDirectReport();
|
||||
directReports2 = m2.getDirectReport();
|
||||
assertEquals(0, directReports1.size());
|
||||
assertEquals(2, directReports2.size());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCopyMethod_whenCopy_thenDistinct() {
|
||||
Date startDate = new Date(123);
|
||||
Employee e1 = new Employee(1, "Baeldung", startDate);
|
||||
Employee e2 = new Employee(e1);
|
||||
List<Employee> directReports = new ArrayList<Employee>();
|
||||
directReports.add(e1);
|
||||
directReports.add(e2);
|
||||
|
||||
// a Manager object whose declaration type is Employee.
|
||||
Employee source = new Manager(1, "Baeldung Manager", startDate, directReports);
|
||||
Employee clone = source.copy();
|
||||
|
||||
// after copy, clone should be still a Manager object.
|
||||
assertTrue(clone instanceof Manager);
|
||||
List<Employee> directReports1 = ((Manager) source).getDirectReport();
|
||||
List<Employee> directReports2 = ((Manager) clone).getDirectReport();
|
||||
assertEquals(directReports1.size(), directReports2.size());
|
||||
assertArrayEquals(directReports1.toArray(), directReports2.toArray());
|
||||
|
||||
// clear source's direct reports list. clone's list should not be affected
|
||||
directReports.clear();
|
||||
directReports1 = ((Manager) source).getDirectReport();
|
||||
directReports2 = ((Manager) clone).getDirectReport();
|
||||
assertEquals(0, directReports1.size());
|
||||
assertEquals(2, directReports2.size());
|
||||
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.booleanoperators;
|
||||
|
||||
public class Car {
|
||||
|
||||
private boolean diesel;
|
||||
private boolean manual;
|
||||
|
||||
public Car(boolean diesel, boolean manual) {
|
||||
this.diesel = diesel;
|
||||
this.manual = manual;
|
||||
}
|
||||
|
||||
public boolean isDiesel() {
|
||||
return diesel;
|
||||
}
|
||||
|
||||
public boolean isManual() {
|
||||
return manual;
|
||||
}
|
||||
|
||||
static Car dieselAndManualCar() {
|
||||
return new Car(true, true);
|
||||
}
|
||||
|
||||
static Car dieselAndAutomaticCar() {
|
||||
return new Car(true, false);
|
||||
}
|
||||
|
||||
static Car oilAndManualCar() {
|
||||
return new Car(false, true);
|
||||
}
|
||||
|
||||
static Car oilAndAutomaticCar() {
|
||||
return new Car(false, false);
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.booleanoperators;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class XorUnitTest {
|
||||
|
||||
@Test
|
||||
void givenDieselManualCar_whenXorOldSchool_thenFalse() {
|
||||
Car car = Car.dieselAndManualCar();
|
||||
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||
assertThat(dieselXorManual).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDieselAutomaticCar_whenXorOldSchool_thenTrue() {
|
||||
Car car = Car.dieselAndAutomaticCar();
|
||||
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||
assertThat(dieselXorManual).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNonDieselManualCar_whenXorOldSchool_thenTrue() {
|
||||
Car car = Car.oilAndManualCar();
|
||||
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||
assertThat(dieselXorManual).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNonDieselAutomaticCar_whenXorOldSchool_thenFalse() {
|
||||
Car car = Car.oilAndAutomaticCar();
|
||||
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
|
||||
assertThat(dieselXorManual).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDieselManualCar_whenXor_thenFalse() {
|
||||
Car car = Car.dieselAndManualCar();
|
||||
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||
assertThat(dieselXorManual).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenDieselAutomaticCar_whenXor_thenTrue() {
|
||||
Car car = Car.dieselAndAutomaticCar();
|
||||
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||
assertThat(dieselXorManual).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNonDieselManualCar_whenXor_thenTrue() {
|
||||
Car car = Car.oilAndManualCar();
|
||||
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||
assertThat(dieselXorManual).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNonDieselAutomaticCar_whenXor_thenFalse() {
|
||||
Car car = Car.oilAndAutomaticCar();
|
||||
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
|
||||
assertThat(dieselXorManual).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNumbersOneAndThree_whenXor_thenTwo() {
|
||||
assertThat(1 ^ 3).isEqualTo(2);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.baeldung.uuid;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UUIDGenerator {
|
||||
@@ -65,9 +66,9 @@ public class UUIDGenerator {
|
||||
try {
|
||||
md = MessageDigest.getInstance("SHA-1");
|
||||
} catch (NoSuchAlgorithmException nsae) {
|
||||
throw new InternalError("MD5 not supported", nsae);
|
||||
throw new InternalError("SHA-1 not supported", nsae);
|
||||
}
|
||||
byte[] bytes = md.digest(name);
|
||||
byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16);
|
||||
bytes[6] &= 0x0f; /* clear version */
|
||||
bytes[6] |= 0x50; /* set to version 5 */
|
||||
bytes[8] &= 0x3f; /* clear variant */
|
||||
|
||||
@@ -2,9 +2,6 @@
|
||||
|
||||
## Java Bean Validation Examples
|
||||
|
||||
###The Course
|
||||
The "REST With Spring" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
- [Java Bean Validation Basics](http://www.baeldung.com/javax-validation)
|
||||
- [Validating Container Elements with Bean Validation 2.0](http://www.baeldung.com/bean-validation-container-elements)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.dto;
|
||||
|
||||
public class CustomerDto {
|
||||
|
||||
private String forename;
|
||||
private String surname;
|
||||
|
||||
public String getForename() {
|
||||
return forename;
|
||||
}
|
||||
|
||||
public CustomerDto setForename(String forename) {
|
||||
this.forename = forename;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public CustomerDto setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
public class Address {
|
||||
|
||||
private String street;
|
||||
private String postalcode;
|
||||
private String county;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public Address setStreet(String street) {
|
||||
this.street = street;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPostalcode() {
|
||||
return postalcode;
|
||||
}
|
||||
|
||||
public Address setPostalcode(String postalcode) {
|
||||
this.postalcode = postalcode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getCounty() {
|
||||
return county;
|
||||
}
|
||||
|
||||
public Address setCounty(String county) {
|
||||
this.county = county;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public Customer setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public Customer setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.entity;
|
||||
|
||||
public class DeliveryAddress {
|
||||
|
||||
private String forename;
|
||||
private String surname;
|
||||
private String street;
|
||||
private String postalcode;
|
||||
private String county;
|
||||
|
||||
public String getForename() {
|
||||
return forename;
|
||||
}
|
||||
|
||||
public DeliveryAddress setForename(String forename) {
|
||||
this.forename = forename;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSurname() {
|
||||
return surname;
|
||||
}
|
||||
|
||||
public DeliveryAddress setSurname(String surname) {
|
||||
this.surname = surname;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public DeliveryAddress setStreet(String street) {
|
||||
this.street = street;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getPostalcode() {
|
||||
return postalcode;
|
||||
}
|
||||
|
||||
public DeliveryAddress setPostalcode(String postalcode) {
|
||||
this.postalcode = postalcode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getCounty() {
|
||||
return county;
|
||||
}
|
||||
|
||||
public DeliveryAddress setCounty(String county) {
|
||||
this.county = county;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
import com.baeldung.dto.CustomerDto;
|
||||
import com.baeldung.entity.Customer;
|
||||
|
||||
@Mapper
|
||||
public interface CustomerDtoMapper {
|
||||
|
||||
@Mapping(source = "firstName", target = "forename")
|
||||
@Mapping(source = "lastName", target = "surname")
|
||||
CustomerDto from(Customer customer);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.mapper;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.MappingTarget;
|
||||
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.Customer;
|
||||
import com.baeldung.entity.DeliveryAddress;
|
||||
|
||||
@Mapper
|
||||
public interface DeliveryAddressMapper {
|
||||
|
||||
@Mapping(source = "customer.firstName", target = "forename")
|
||||
@Mapping(source = "customer.lastName", target = "surname")
|
||||
@Mapping(source = "address.street", target = "street")
|
||||
@Mapping(source = "address.postalcode", target = "postalcode")
|
||||
@Mapping(source = "address.county", target = "county")
|
||||
DeliveryAddress from(Customer customer, Address address);
|
||||
|
||||
@Mapping(source = "address.postalcode", target = "postalcode")
|
||||
@Mapping(source = "address.county", target = "county")
|
||||
DeliveryAddress updateAddress(@MappingTarget DeliveryAddress deliveryAddress, Address address);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.mapper;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import com.baeldung.dto.CustomerDto;
|
||||
import com.baeldung.entity.Customer;
|
||||
|
||||
public class CustomerDtoMapperUnitTest {
|
||||
|
||||
private CustomerDtoMapper customerDtoMapper = Mappers.getMapper(CustomerDtoMapper.class);
|
||||
|
||||
@Test
|
||||
void testGivenCustomer_mapsToCustomerDto() {
|
||||
|
||||
// given
|
||||
Customer customer = new Customer().setFirstName("Max")
|
||||
.setLastName("Powers");
|
||||
|
||||
// when
|
||||
CustomerDto customerDto = customerDtoMapper.from(customer);
|
||||
|
||||
// then
|
||||
assertEquals(customerDto.getForename(), customer.getFirstName());
|
||||
assertEquals(customerDto.getSurname(), customer.getLastName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.mapper;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import com.baeldung.entity.Address;
|
||||
import com.baeldung.entity.Customer;
|
||||
import com.baeldung.entity.DeliveryAddress;
|
||||
|
||||
public class DeliveryAddressMapperUnitTest {
|
||||
|
||||
private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class);
|
||||
|
||||
@Test
|
||||
public void testGivenCustomerAndAddress_mapsToDeliveryAddress() {
|
||||
|
||||
// given
|
||||
Customer customer = new Customer().setFirstName("Max")
|
||||
.setLastName("Powers");
|
||||
|
||||
Address homeAddress = new Address().setStreet("123 Some Street")
|
||||
.setCounty("Nevada")
|
||||
.setPostalcode("89123");
|
||||
|
||||
// when
|
||||
DeliveryAddress deliveryAddress = deliveryAddressMapper.from(customer, homeAddress);
|
||||
|
||||
// then
|
||||
assertEquals(deliveryAddress.getForename(), customer.getFirstName());
|
||||
assertEquals(deliveryAddress.getSurname(), customer.getLastName());
|
||||
assertEquals(deliveryAddress.getStreet(), homeAddress.getStreet());
|
||||
assertEquals(deliveryAddress.getCounty(), homeAddress.getCounty());
|
||||
assertEquals(deliveryAddress.getPostalcode(), homeAddress.getPostalcode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGivenDeliveryAddressAndSomeOtherAddress_updatesDeliveryAddress() {
|
||||
|
||||
// given
|
||||
Customer customer = new Customer().setFirstName("Max")
|
||||
.setLastName("Powers");
|
||||
|
||||
DeliveryAddress deliveryAddress = new DeliveryAddress().setStreet("123 Some Street")
|
||||
.setCounty("Nevada")
|
||||
.setPostalcode("89123");
|
||||
|
||||
Address otherAddress = new Address().setStreet("456 Some other street")
|
||||
.setCounty("Arizona")
|
||||
.setPostalcode("12345");
|
||||
|
||||
// when
|
||||
DeliveryAddress updatedDeliveryAddress = deliveryAddressMapper.updateAddress(deliveryAddress, otherAddress);
|
||||
|
||||
// then
|
||||
assertSame(deliveryAddress, updatedDeliveryAddress);
|
||||
|
||||
assertEquals(deliveryAddress.getStreet(), otherAddress.getStreet());
|
||||
assertEquals(deliveryAddress.getCounty(), otherAddress.getCounty());
|
||||
assertEquals(deliveryAddress.getPostalcode(), otherAddress.getPostalcode());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
### Logistic Regression in Java
|
||||
This is a soft introduction to ML using [deeplearning4j](https://deeplearning4j.org) library
|
||||
|
||||
### Relevant Articles:
|
||||
- [Logistic Regression in Java](http://www.baeldung.com/)
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.deeplearning4j</groupId>
|
||||
<artifactId>ml</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>Machine Learning</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.nd4j</groupId>
|
||||
<artifactId>nd4j-native-platform</artifactId>
|
||||
<version>${dl4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.deeplearning4j</groupId>
|
||||
<artifactId>deeplearning4j-core</artifactId>
|
||||
<version>${dl4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.deeplearning4j</groupId>
|
||||
<artifactId>deeplearning4j-nn</artifactId>
|
||||
<version>${dl4j.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.datavec/datavec-api -->
|
||||
<dependency>
|
||||
<groupId>org.datavec</groupId>
|
||||
<artifactId>datavec-api</artifactId>
|
||||
<version>${dl4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.3.5</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<dl4j.version>1.0.0-beta4</dl4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,168 @@
|
||||
package com.baeldung.logreg;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import org.datavec.api.io.labels.ParentPathLabelGenerator;
|
||||
import org.datavec.api.split.FileSplit;
|
||||
import org.datavec.image.loader.NativeImageLoader;
|
||||
import org.datavec.image.recordreader.ImageRecordReader;
|
||||
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
|
||||
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
|
||||
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
|
||||
import org.deeplearning4j.nn.conf.inputs.InputType;
|
||||
import org.deeplearning4j.nn.conf.layers.ConvolutionLayer;
|
||||
import org.deeplearning4j.nn.conf.layers.DenseLayer;
|
||||
import org.deeplearning4j.nn.conf.layers.OutputLayer;
|
||||
import org.deeplearning4j.nn.conf.layers.SubsamplingLayer;
|
||||
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
||||
import org.deeplearning4j.nn.weights.WeightInit;
|
||||
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
|
||||
import org.deeplearning4j.util.ModelSerializer;
|
||||
import org.nd4j.evaluation.classification.Evaluation;
|
||||
import org.nd4j.linalg.activations.Activation;
|
||||
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
|
||||
import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
|
||||
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
|
||||
import org.nd4j.linalg.learning.config.Nesterovs;
|
||||
import org.nd4j.linalg.lossfunctions.LossFunctions;
|
||||
import org.nd4j.linalg.schedule.MapSchedule;
|
||||
import org.nd4j.linalg.schedule.ScheduleType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Handwritten digit image classification based on LeNet-5 architecture by Yann LeCun.
|
||||
*
|
||||
* This code accompanies the article "Logistic regression in Java" and is heavily based on
|
||||
* <a href="https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/convolution/mnist/MnistClassifier.java">MnistClassifier</a>.
|
||||
* Some minor changes have been made in order to make article's flow smoother.
|
||||
*
|
||||
*/
|
||||
|
||||
public class MnistClassifier {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MnistClassifier.class);
|
||||
private static final String basePath = System.getProperty("java.io.tmpdir") + "mnist" + File.separator;
|
||||
private static final File modelPath = new File(basePath + "mnist-model.zip");
|
||||
private static final String dataUrl = "http://github.com/myleott/mnist_png/raw/master/mnist_png.tar.gz";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// input image sizes in pixels
|
||||
int height = 28;
|
||||
int width = 28;
|
||||
// input image colour depth (1 for gray scale images)
|
||||
int channels = 1;
|
||||
// the number of output classes
|
||||
int outputClasses = 10;
|
||||
// number of samples that will be propagated through the network in each iteration
|
||||
int batchSize = 54;
|
||||
// total number of training epochs
|
||||
int epochs = 1;
|
||||
|
||||
// initialize a pseudorandom number generator
|
||||
int seed = 1234;
|
||||
Random randNumGen = new Random(seed);
|
||||
|
||||
final String path = basePath + "mnist_png" + File.separator;
|
||||
if (!new File(path).exists()) {
|
||||
logger.info("Downloading data {}", dataUrl);
|
||||
String localFilePath = basePath + "mnist_png.tar.gz";
|
||||
File file = new File(localFilePath);
|
||||
if (!file.exists()) {
|
||||
file.getParentFile()
|
||||
.mkdirs();
|
||||
Utils.downloadAndSave(dataUrl, file);
|
||||
Utils.extractTarArchive(file, basePath);
|
||||
}
|
||||
} else {
|
||||
logger.info("Using the local data from folder {}", path);
|
||||
}
|
||||
|
||||
logger.info("Vectorizing the data from folder {}", path);
|
||||
// vectorization of train data
|
||||
File trainData = new File(path + "training");
|
||||
FileSplit trainSplit = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
|
||||
// use parent directory name as the image label
|
||||
ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator();
|
||||
ImageRecordReader trainRR = new ImageRecordReader(height, width, channels, labelMaker);
|
||||
trainRR.initialize(trainSplit);
|
||||
DataSetIterator train = new RecordReaderDataSetIterator(trainRR, batchSize, 1, outputClasses);
|
||||
|
||||
// pixel values from 0-255 to 0-1 (min-max scaling)
|
||||
DataNormalization imageScaler = new ImagePreProcessingScaler();
|
||||
imageScaler.fit(train);
|
||||
train.setPreProcessor(imageScaler);
|
||||
|
||||
// vectorization of test data
|
||||
File testData = new File(path + "testing");
|
||||
FileSplit testSplit = new FileSplit(testData, NativeImageLoader.ALLOWED_FORMATS, randNumGen);
|
||||
ImageRecordReader testRR = new ImageRecordReader(height, width, channels, labelMaker);
|
||||
testRR.initialize(testSplit);
|
||||
DataSetIterator test = new RecordReaderDataSetIterator(testRR, batchSize, 1, outputClasses);
|
||||
// same normalization for better results
|
||||
test.setPreProcessor(imageScaler);
|
||||
|
||||
logger.info("Network configuration and training...");
|
||||
// reduce the learning rate as the number of training epochs increases
|
||||
// iteration #, learning rate
|
||||
Map<Integer, Double> learningRateSchedule = new HashMap<>();
|
||||
learningRateSchedule.put(0, 0.06);
|
||||
learningRateSchedule.put(200, 0.05);
|
||||
learningRateSchedule.put(600, 0.028);
|
||||
learningRateSchedule.put(800, 0.0060);
|
||||
learningRateSchedule.put(1000, 0.001);
|
||||
|
||||
final ConvolutionLayer layer1 = new ConvolutionLayer.Builder(5, 5).nIn(channels)
|
||||
.stride(1, 1)
|
||||
.nOut(20)
|
||||
.activation(Activation.IDENTITY)
|
||||
.build();
|
||||
final SubsamplingLayer layer2 = new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2)
|
||||
.stride(2, 2)
|
||||
.build();
|
||||
// nIn need not specified in later layers
|
||||
final ConvolutionLayer layer3 = new ConvolutionLayer.Builder(5, 5).stride(1, 1)
|
||||
.nOut(50)
|
||||
.activation(Activation.IDENTITY)
|
||||
.build();
|
||||
final DenseLayer layer4 = new DenseLayer.Builder().activation(Activation.RELU)
|
||||
.nOut(500)
|
||||
.build();
|
||||
final OutputLayer layer5 = new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputClasses)
|
||||
.activation(Activation.SOFTMAX)
|
||||
.build();
|
||||
final MultiLayerConfiguration config = new NeuralNetConfiguration.Builder().seed(seed)
|
||||
.l2(0.0005) // ridge regression value
|
||||
.updater(new Nesterovs(new MapSchedule(ScheduleType.ITERATION, learningRateSchedule)))
|
||||
.weightInit(WeightInit.XAVIER)
|
||||
.list()
|
||||
.layer(layer1)
|
||||
.layer(layer2)
|
||||
.layer(layer3)
|
||||
.layer(layer2)
|
||||
.layer(layer4)
|
||||
.layer(layer5)
|
||||
.setInputType(InputType.convolutionalFlat(height, width, channels))
|
||||
.build();
|
||||
|
||||
final MultiLayerNetwork model = new MultiLayerNetwork(config);
|
||||
model.init();
|
||||
model.setListeners(new ScoreIterationListener(100));
|
||||
logger.info("Total num of params: {}", model.numParams());
|
||||
|
||||
// evaluation while training (the score should go down)
|
||||
for (int i = 0; i < epochs; i++) {
|
||||
model.fit(train);
|
||||
logger.info("Completed epoch {}", i);
|
||||
train.reset();
|
||||
test.reset();
|
||||
}
|
||||
Evaluation eval = model.evaluate(test);
|
||||
logger.info(eval.stats());
|
||||
|
||||
ModelSerializer.writeModel(model, modelPath, true);
|
||||
logger.info("The MINIST model has been saved in {}", modelPath.getPath());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.logreg;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JFileChooser;
|
||||
|
||||
import org.datavec.image.loader.NativeImageLoader;
|
||||
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
|
||||
import org.deeplearning4j.util.ModelSerializer;
|
||||
import org.nd4j.linalg.api.ndarray.INDArray;
|
||||
import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MnistPrediction {
|
||||
private static final Logger logger = LoggerFactory.getLogger(MnistPrediction.class);
|
||||
private static final File modelPath = new File(System.getProperty("java.io.tmpdir") + "mnist" + File.separator + "mnist-model.zip");
|
||||
private static final int height = 28;
|
||||
private static final int width = 28;
|
||||
private static final int channels = 1;
|
||||
|
||||
/**
|
||||
* Opens a popup that allows to select a file from the filesystem.
|
||||
* @return
|
||||
*/
|
||||
public static String fileChose() {
|
||||
JFileChooser fc = new JFileChooser();
|
||||
int ret = fc.showOpenDialog(null);
|
||||
if (ret == JFileChooser.APPROVE_OPTION) {
|
||||
File file = fc.getSelectedFile();
|
||||
return file.getAbsolutePath();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (!modelPath.exists()) {
|
||||
logger.info("The model not found. Have you trained it?");
|
||||
return;
|
||||
}
|
||||
MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelPath);
|
||||
String path = fileChose();
|
||||
File file = new File(path);
|
||||
|
||||
INDArray image = new NativeImageLoader(height, width, channels).asMatrix(file);
|
||||
new ImagePreProcessingScaler(0, 1).transform(image);
|
||||
|
||||
// Pass through to neural Net
|
||||
INDArray output = model.output(image);
|
||||
|
||||
logger.info("File: {}", path);
|
||||
logger.info("Probabilities: {}", output);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.baeldung.logreg;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.commons.compress.archivers.ArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
|
||||
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Utility class for digit classifier.
|
||||
*
|
||||
*/
|
||||
public class Utils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
|
||||
|
||||
private Utils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Download the content of the given url and save it into a file.
|
||||
* @param url
|
||||
* @param file
|
||||
*/
|
||||
public static void downloadAndSave(String url, File file) throws IOException {
|
||||
CloseableHttpClient client = HttpClientBuilder.create()
|
||||
.build();
|
||||
logger.info("Connecting to {}", url);
|
||||
try (CloseableHttpResponse response = client.execute(new HttpGet(url))) {
|
||||
HttpEntity entity = response.getEntity();
|
||||
if (entity != null) {
|
||||
logger.info("Downloaded {} bytes", entity.getContentLength());
|
||||
try (FileOutputStream outstream = new FileOutputStream(file)) {
|
||||
logger.info("Saving to the local file");
|
||||
entity.writeTo(outstream);
|
||||
outstream.flush();
|
||||
logger.info("Local file saved");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a "tar.gz" file into a given folder.
|
||||
* @param file
|
||||
* @param folder
|
||||
*/
|
||||
public static void extractTarArchive(File file, String folder) throws IOException {
|
||||
logger.info("Extracting archive {} into folder {}", file.getName(), folder);
|
||||
// @formatter:off
|
||||
try (FileInputStream fis = new FileInputStream(file);
|
||||
BufferedInputStream bis = new BufferedInputStream(fis);
|
||||
GzipCompressorInputStream gzip = new GzipCompressorInputStream(bis);
|
||||
TarArchiveInputStream tar = new TarArchiveInputStream(gzip)) {
|
||||
// @formatter:on
|
||||
TarArchiveEntry entry;
|
||||
while ((entry = (TarArchiveEntry) tar.getNextEntry()) != null) {
|
||||
extractEntry(entry, tar, folder);
|
||||
}
|
||||
}
|
||||
logger.info("Archive extracted");
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract an entry of the input stream into a given folder
|
||||
* @param entry
|
||||
* @param tar
|
||||
* @param folder
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void extractEntry(ArchiveEntry entry, InputStream tar, String folder) throws IOException {
|
||||
final int bufferSize = 4096;
|
||||
final String path = folder + entry.getName();
|
||||
if (entry.isDirectory()) {
|
||||
new File(path).mkdirs();
|
||||
} else {
|
||||
int count;
|
||||
byte[] data = new byte[bufferSize];
|
||||
// @formatter:off
|
||||
try (FileOutputStream os = new FileOutputStream(path);
|
||||
BufferedOutputStream dest = new BufferedOutputStream(os, bufferSize)) {
|
||||
// @formatter:off
|
||||
while ((count = tar.read(data, 0, bufferSize)) != -1) {
|
||||
dest.write(data, 0, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user