2022年5月27日 星期五

switch ???

 #include <iostream>

using namespace std;

int main()

{

    int i =0;

    for (i=0;i<20;i++)

    {

        switch(i)

        {

            case 0:

            i+=5;

            cout<< i<<" ";

            case 1:

            i+=2;

            cout<< i<<" ";

            case 5:

            i+=5;

            cout<< i<<" ";

            default:

            i+=4;

            cout<< i<<" ";

            break;

        }

            cout<< i<<" ";

    }

    return 0;

}


//ans: 5 7 12 16 16 21 21 

2022年5月15日 星期日

dag

 graph = {

    'A':['B','C'],

    'B':['D','E'],

    'C':['E'],

    'D':['F'],

    'E':['F'],

    'F':[]    

    }


visited = set()

visiteds = []


def dfs(graph,visited,node):

    visiteds.append(node)

    if node not in visited:

        visited.add(node)

        print(node,end=' ')

        for neighbor in graph[node]:

            dfs(graph,visited,neighbor)


dfs(graph,visited,'A')

print(visiteds)

dag = set([i for i in visiteds if visiteds.count(i)>1])

print(dag)

2022年5月14日 星期六

prime

 # sol1

# def prime(n):

#     for  i in range(2,n+1):

#         j = 2

#         while  j<i:

#             if i%j==0:

#                 break

#             j+=1

#         if j==i:

#             print(i,end=', ')

# prime(100)


# sol2

p = [2]

for i in range(3,101):

    isp = True

    for j in p:

        if i%j==0:

            isp = False

            break

    if isp==True:

        p.append(i)

p = map(str,p)

p = ', '.join(p)

print(p)

2022年5月10日 星期二

二長方形重疊區域面積

 def dist(a,b,c,d):

    over = 0

    if b>=d>=a>=c: over=d-a

    elif d>=b>=a>=c : over = b-a

    elif b>=d>=c>=a : over = d-c

    elif d>=b>=c>=a:over = b-c

    else : over=0

    return over

x1,y1,w1,h1 = map(int,input().strip().split())

x2,y2,w2,h2 = map(int,input().strip().split())


xdist = dist(x1,x1+w1,x2,x2+w2)

ydist = dist(y1,y1+h1,y2,y2+h2)

area = xdist*ydist

print(area)

2022年5月5日 星期四

生成對抗網路 GAN

 

生成對抗網路


人工智慧

讓機器具有如同人類的思辨能力

機器學習

能夠達成人工智慧的方法,透過與人類相似的學習方法,訓練機器進行資料分類、處理與預測

深度學習

代表實現機器學習的技術,深層、多層類神經網路。

GAN由兩個網路構成

鑑別網路(Discriminating Network

生成網路(Generative Network

透過兩者相互對抗產生結果是其深度學習的運作原理。

比喻

GAN是一場鑑定師與仿畫家的比賽,仿畫家畫出假畫讓鑑定師評斷有多接近真品,根據評斷結果再繼續畫出比原本更好的作品,鑑定師也會透過不斷練習提升鑑定水準,最後結果就是一幅幾可亂真的機器畫。

卷積神經網路(Convoulutional Neural Network

適合處理空間資料

循環神經網路(Ruccurent Neural Network

擅長處理時間序列與語意結構判斷

對抗訓練是有史以來最酷的技術

FacebookAI研究院長楊立昆(Yann LeCun

對抗訓練是有史以來最酷的技術(Adversarial training is the coolest thing since sliced bread),對於GAN他也說:「它為創建無監督學習模型提供了強有力的算法框架,有望幫助我們為AI加入常識(common sense

GAN的應用

GAN應用在生成資料方面,如圖像與影音的生成、合成、辨識、修復等等,

進階則是輸入文本描述便能生成與形容相符的圖像,或者透過語言模型實現機器翻譯等。

Nvidia公司的GAN則是將白天的街景轉為夜晚,作為自動駕駛車輛的訓練樣本,解決夜晚圖像資料不足的問題。

展望

GAN在未來依舊可能真正實現「完全不靠人類就能自主學習的AI

 

生成對抗網路II

1.          非監督式學習的一種方法

2.          讓兩個神經網路相互博弈的方式進行學習。

3.          由一個生成網絡與一個判別網絡組成。

4.          從潛在空間(latent space)中隨機取樣作為輸入,其輸出結果需要盡量模仿訓練集中的真實樣本。

5.          判別網絡的輸入則為真實樣本或生成網絡的輸出,其目的是將生成網絡的輸出從真實樣本中盡可能分辨出來。

6.          而生成網絡則要盡可能地欺騙判別網絡。

7.          兩個網絡相互對抗、不斷調整參數,最終目的是使判別網絡無法判斷生成網絡的輸出結果是否真實。

 

 

 

資料來源:

1.          https://medium.com/@hiskio/%E7%94%9F%E6%88%90%E5%B0%8D%E6%8A%97%E7%B6%B2%E8%B7%AF%E5%88%B0%E5%BA%95%E5%9C%A8gan%E9%BA%BB-f149efb9eb6b

2.          https://zh.wikipedia.org/zh-tw/%E7%94%9F%E6%88%90%E5%AF%B9%E6%8A%97%E7%BD%91%E7%BB%9C


2022年5月1日 星期日

lcs

 def lcs(x,y,m,n):

    if m==0 or n==0:
        return 0
    elif (x[m-1]==y[n-1]):
        return lcs(x,y,m-1,n-1)+1
    else:
        return max(lcs(x,y,m,n-1), lcs(x,y,m-1,n))

x = 'abcd'
y = 'abdef'
print(lcs(x,y,len(x),len(y)))

參考資料來源:https://seanleetech.com/algorithm/47/

lis

 def lis(a):

    n = len(a)
    lis = [1]*n
    for i in range(1,n):
        for j in range(0,i):
            if a[j]<a[i]:
                lis[i] = max(lis[i],lis[j]+1)
    m = 0
    
    for i in range(n):
        m = max(m,lis[i])
    return m
a = [1,2,9,3,10,5,4,6]
print(lis(a))

字串小程式

 s = 'abcdeckba'

n = len(s)
a=[]

 # test5

# def f(s,c,n):
#     if c==n:
#         print()
#         return
#     print(s[c],end=' ')
#     return f(s,c+1,n)
# f(s,0,n)

# test4
# def f(s,c,n):
#     if c==n:
#         print()
#         return
#     print(s[c:]+s[:c],end=' ')
#     return f(s,c+1,n)
# f(s,0,n)

# test3
# def f(s,c,n):
#     if c==n:
#         print()
#         return
#     print(s[c:]+s[:c],end=' ')
#     return f(s,c+1,n)
# f(s,0,n)
# test1
# def f(s,x):
#     if x==n:
#         return
#     a.append(s[:x])
#     return f(s,x+1)
# f(s,0)
# print(a)

# test2
# for p in range(len(s)):
#     # p = n//2
#     rStr = s[p]
#     preStr =s[:p]
#     preStr = preStr[::-1]
#     postStr = s[p+1:]
#     # print(preStr,postStr)
#     for i in preStr:
#         if i in postStr:
#             if abs(preStr.find(i)- postStr.find(i))<=2:
#                 rStr = i + rStr + i
#                 t = list(preStr)
#                 t = t.remove(i)
#                 # print(t)
#                 if t:
#                     preStr = ''.join(t)
#                 else:
#                     preStr = ''
#                 t = list(postStr)
#                 t = t.remove(i)
#                 if t:
#                     postStr = ''.join(t)
#                 else:
#                     postStr = ''
#     print(rStr)

子字串回文

 s = 'abcdeckba'

n = len(s)
a=[]

def f1(s,rstr,p,n):
    if p==n: return
    if p==n//2:a.append(s[p])
    t = len(s)//2-len(rstr)//2+1
    # print(t)
    if s[p] in s[:t]:
        if len(rstr)<1 and s[p-1] != s[p]: 
            rstr = rstr+s[p]
        else:
            rstr = s[p]+rstr+s[p]
            a.append(rstr)
    print(rstr,end=' ')
    f1(s,rstr,p+1,n)
    return
f1(s,'',len(s)//2,n)
print(a)
aLen = [len(i) for i in a]
print(aLen) 
aLenMax = max(aLen)
print(aLenMax)
idx = aLen.index(aLenMax)
print(a[idx])