[Deep learning] Gradient Descent Optimizer

이미지
https://icim.nims.re.kr/post/easyMath/70 알기 쉬운 산업수학 알기 쉬운 산업수학 Gradient Descent Optimizer 2017년 12월 13일(수) | 김민중 URL  (1) 주어진 목적함수  f f 의 최솟값(minimum)을 찾아가는 알고리즘으로 다음과 같은 방식으로 최솟값을 찾아간다. 초기값  x 0 x 0 을 임의로 설정, 적당한 Learning rate  α α  설정 n ≥ 0 n ≥ 0 인 정수에 대해서  x n + 1 x n + 1 은 다음과 같이 정의한다. x n + 1 : = x n − α ⋅ ∇ f ( x n ) x n + 1 := x n − α ⋅ ∇ f ( x n ) 주의사항 함수 f f 의 모양이 convex가 아닌 경우 global minimum이 아닌 local minimum으로  x n x n 이 수렴할 가능성이 있다. Learning rate  α α  값이 큰 경우 최솟값으로  x n x n 이 수렴하는 것이 아니라 발산할 수 있다. Learning rate  α α  값이 작은 경우 수렴하는 속도가 지나치게 느릴 수 있다. 방정식  2 ⋅ x = 10 2 ⋅ x = 10  의 근을 Gradient Descent를 이용해서 찾아보자. 목적함수  f ( x ) : = ( 10 − 2 x ) 2 f ( x ) := ( 10 − 2 x ) 2 으로 설정하겠다( f f 의 최솟값인 0이 되게 하는  x x 값이 우리가 원하는 방정식의 근이다). 초기값  x 0 = 0 x 0 = 0 , Learning rate  α = 0.05 α = 0.05 으로 설정 f ′ ( x ) = 4 ( 2 x − 10 ) f ′ ( x ) = 4 ( 2 x − 10 )...

[Android] Android Read SQLite database From Assets Folder




Android Read SQLite database From Assets Folder


In this tutorial you will learn,  how can you read SQLite Database from assets folder? Some times you have a SQLite database file and want to read in your app. So this tutorial explain you about it.
In this example we need a .sqlite file and we put this file inside assets folder. Now lets start this example
Create activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
<ScrollView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="10dp">
<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical">
<TextView
 android:id="@+id/readTV"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Hello World!" />
 </LinearLayout>
 </ScrollView>
</RelativeLayout>
Add mydb.sqlite file inside assets folder.
Create model class Contact.java file 
package com.androideve.assetsdata;

public class Contact {

public int id;
 public String name = "";
 public String hint = "";

public Contact(int id, String name, String hint) {
 this.id = id;
 this.name = name;
 this.hint = hint;
 }

public int getId() {
 return id;
 }
public void setId(int id) {
 this.id = id;
 }
public String getName() {
 return name;
 }
public void setName(String name) {
 this.name = name;
 }
public String getHint() {
 return hint;
 }
public void setHint(String hint) {
 this.hint = hint;
 }
}
Create SQLite database helper class SqlLiteDbHelper.java file
package com.androideve.assetsdata;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqlLiteDbHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
 private static final String DATABASE_NAME = "mydb.sqlite";
 private static final String DB_PATH_SUFFIX = "/databases/";
 static Context ctx;
public SqlLiteDbHelper(Context context) {
 super(context, DATABASE_NAME, null, DATABASE_VERSION);
 ctx = context;
 }
public ArrayList<Contact> getDetails() {
 SQLiteDatabase db = this.getReadableDatabase();
 ArrayList<Contact> contList = new ArrayList<>();
 Cursor cursor = db.rawQuery("SELECT * FROM demo", null);
 if (cursor != null) {
 while (cursor.moveToNext()) {
Contact cont = new Contact(cursor.getInt(0), cursor.getString(1), cursor.getString(2));
 contList.add(cont);
}
 cursor.close();
 db.close();
 }
 return contList;
 }
public void CopyDataBaseFromAsset() throws IOException {
InputStream myInput = ctx.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
 String outFileName = getDatabasePath();

// if the path doesn't exist first, create it
 File f = new File(ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
 if (!f.exists())
 f.mkdir();

// Open the empty db as the output stream
 OutputStream myOutput = new FileOutputStream(outFileName);

// transfer bytes from the inputfile to the outputfile
 byte[] buffer = new byte[1024];
 int length;
 while ((length = myInput.read(buffer)) > 0) {
 myOutput.write(buffer, 0, length);
 }
// Close the streams
 myOutput.flush();
 myOutput.close();
 myInput.close();

}
private static String getDatabasePath() {
 return ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX
 + DATABASE_NAME;
 }
public SQLiteDatabase openDataBase() throws SQLException {
 File dbFile = ctx.getDatabasePath(DATABASE_NAME);
if (!dbFile.exists()) {
 try {
 CopyDataBaseFromAsset();
 System.out.println("Copying sucess from Assets folder");
 } catch (IOException e) {
 throw new RuntimeException("Error creating source database", e);
 }
 }
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
 }
@Override
 public void onCreate(SQLiteDatabase db) {
}
@Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 }
}
Create MainActivity.java file to retrieve data from Database
package com.androideve.assetsdata;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

TextView readTV;
 SqlLiteDbHelper dbHelper;
 ArrayList<Contact> contList;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

readTV = (TextView) findViewById(R.id.readTV);
 contList = new ArrayList<>();
 contList.clear();
 dbHelper = new SqlLiteDbHelper(this);
 dbHelper.openDataBase();
 contList = dbHelper.getDetails();
 String msg ="";
 for(int i=0;i<contList.size();i++){

Contact cont = contList.get(i);
 msg = msg+cont.getId()+" "+cont.getName()+" "+cont.getHint()+"\n\n\n";
 readTV.setText(msg);
 }
 }
}

Happy Coding !


<Reference>
http://androideve.com/2017/02/android-read-sqlite-databse-assets-folder/

댓글

이 블로그의 인기 게시물

@import' when modules are disabled

MagicaVoxel black screen

[Anaconda][Opencv][Spyder] How to install opencv to anaconda (spyder).