1. 程式人生 > >Brian模擬器,關於MagicError的原因及解決方法

Brian模擬器,關於MagicError的原因及解決方法

檢視官方文件:https://brian2.readthedocs.io/en/2.0rc/user/running.html

對於構建的SNN,Brian有多種執行方法,在大多數簡單的模擬中,您不必顯式地建立Network物件(參考官方手冊intro3-simulator有用到Network的例子),而是可以簡單地呼叫run()來執行模擬。這就是所謂的'Magic'系統,因為brain會自動知道你想做什麼。

然而多次執行後往往會得到報錯:

MagicError: The magic network contains a mix of objects that has been run before and new objects, Brian does not know whether you want to start a new simulation or continue an old one. Consider explicitly creating a Network object. Also note that you can find out which objects will be included in a magic network with the collect() function.

翻譯:magic network包含以前執行過的物件和新物件的組合,Brian不知道是要開始一個新的模擬還是繼續一箇舊的模擬。考慮顯式地建立網路物件。(這是一種解決方法,但對程式碼改動較大)

第二種方法是在程式碼段前面加上start_scope()

start_scope()
    '''
    Starts a new scope for magic functions
    
    All objects created before this call will no longer be automatically
    included by the magic functions such as `run`.
    '''

這個方法的作用是使模擬器忽略前面的模擬。

第三種方法是直接在spyder的Python console中選擇清除所有變數,這個方法比較簡單粗暴。