今天我們來講講怎麼優化文本生成。
num_beams=1, do_sample=False
。程式碼如下:max_length = 128
### 昨天的句子太簡單不好玩,今天改成比特幣創世區塊的新聞稿
input_txt = """
Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens. \n
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that \
the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
"""
input_ids = tokenizer(input_txt, return_tensors="pt")["input_ids"].to(device)
output = model.generate(input_ids, max_length=max_length, num_beams=1, do_sample=False)
print(tokenizer.decode(output[0]))
會得到:
Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
The Chancellor will also consider whether to take a second look at the Bank of England's plans to raise interest rates, which have been held at a record low of 0.5 per cent since March 2009.
The Chancellor will also consider whether to take a second look at the Bank of England's plans to raise
我的句子中的 Typo Cancellor
,居然自動生成為 Chancellor
了,真是太強大!
max_length = 128
變成 256 再看看,會得到下面的結果:Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
The Chancellor will also consider whether to take a second look at the Bank of England's plans to raise interest rates, which have been held at a record low of 0.5 per cent since March 2009.
The Chancellor will also consider whether to take a second look at the Bank of England's plans to raise interest rates, which have been held at a record low of 0.5 per cent since March 2009.
The Chancellor will also consider whether to take a second look at the Bank of England's plans to raise interest rates, which have been held at a record low of 0.5 per cent since March 2009.
The Chancellor will also consider whether to take a second look at the Bank of England's plans to raise interest rates, which have been held at a record low of 0.5 per cent since March 2009.
The Chancellor will also consider whether to take a second look at the Bank of England's plans to
num_beams
。如果我們就來設定為 num_beams=3
,會得到下面的結果Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
Mr Darling said: "We have to look at what we can do to help the banks.
"We have to look at what we can do to help the economy.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the economy.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the banks.
"We have to look at what we can do to help the banks.
no_repeat_ngram_size
來優化,就設定no_repeat_ngram_size=5
吧,會得下面的結果。Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
Mr Darling said: "We have to look at what we can do to help the banks.
"We have to think about what we can do for the economy.
"It is not just about the banks, it is about the economy as a whole.
"If we don't do something to help the banks, the economy is going to suffer."
He added: "We have got to get the banks lending again.
num_beams=1, do_sample=True
temperature
來提高輸出的多樣性。設定temperature=1.5
來玩玩看吧!我們可以得到下面的結果:Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
Darling told The Times there might be options including:
RBS to provide capital on the equivalent of 30pc to 40pc loans: £5billion for the bank: and in lieu of the cash the Treasury agreed to write off a loan of 10bn from Lloyds.
He added: "It's very critical we help the struggling British institutions, rather than just a very narrowly-tied solution."
He said the government had only put up 10 billion in funds – the Bank's 40pc – so would "need another 20 or 30 billion" to bridge an already yawning £120bn hole to public coffers.<|endoftext|>
這兩位是另一種 Sampling 的手法,一樣是從機率分布中抽樣,但是可以避免一些機率出現的字詞,
do_sample=True, top_k=50
來玩玩看,會得到下面的結果。Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
Ministers last night confirmed they were trying to get a green light to extend the Emergency Liquidity Assistance (ELA) that banks were forced to take from last year.
(REUTERS) – Europe's second largest economy faces a major threat to its finances, with the country's banks due to borrow around 27 billion euros of fresh funds from the government to keep their operations afloat this year, a week before they are expected to be sold. If approved, the government would also be forced to spend around 10 billion euros to prop up the weak banking sector, which already sits below 3.5 billion euros.
"In the coming weeks we will have to consider whether it makes sense to go for another ELA," a senior government source said.
The announcement of a third "stress test" – a test conducted by the EU's banking watchdog the European Banking Authority (EBA) – came as a shock to eurozone investors, who
我們可以看到產生的內容好像更加一回事了。
do_sample=True, top_p=0.95
來玩玩看,會得到下面的文本:Alistair Darling has been forced to consider a second bailout for banks as the lending drought worsens.
The Cancellor will decide tithin weeks whether to pump billions more into the economy as evidence mounts that the 37 billion part-nationalisation last yearr has failed to keep credit flowing,
Mr Darling, the former Liberal Democrat chancellor, admitted that the situation had become critical but insisted that there was still time to turn things around.
He told the BBC that the crisis in the banking sector was the most serious problem facing the economy but also highlighted other issues, such as the falling value of sterling and the threat of inflation.
"The worst fears about the banking crisis seem not to be panning out," he said, adding that there had not been a single banker arrested or charged over the crash.
The economy, the economy
Mr Darling said "there's been a very, very strong recovery" since the autumn of 2008.
"There are very big problems ahead of us, not least of which is inflation. It is likely to be a very high inflation rate. "
The economy is expected to grow by 0.3% in the quarter to the end of this year,
以上就是文本生成常用到的一些手法了,大家可以盡情地排列組合,以調整出最適合你應用的參數。如果是精確任務或為特定問題提供答案,那麼降低溫度和使用 greedy 或 beam search 是比較好的方法。如果是生成更長的文本,甚至有點創意,則可以用 Sampling 的方法。
其實在做 Greedy 時,出現 the Bank of England
,我已經又驚又喜了,因為那串文本的下一段,就是 The Bank of England 開頭的段落。